-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler28.py
More file actions
executable file
·43 lines (33 loc) · 764 Bytes
/
euler28.py
File metadata and controls
executable file
·43 lines (33 loc) · 764 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def move_right(x,y):
return x+1, y
def move_down(x,y):
return x,y-1
def move_left(x,y):
return x-1,y
def move_up(x,y):
return x,y+1
moves = [move_right, move_down, move_left, move_up]
def gen_points(end):
from itertools import cycle
_moves = cycle(moves)
n = 1
pos = 0,0
times_to_move = 1
yield n,pos
while True:
for _ in range(2):
move = next(_moves)
for _ in range(times_to_move):
if n >= end:
return
pos = move(*pos)
n+=1
yield n,pos
times_to_move+=1
spiral = list(gen_points(1002001))
total = 0
for a,b in spiral:
i,j = b
if abs(i) == abs(j):
total += a
print total