-
Notifications
You must be signed in to change notification settings - Fork 0
/
p28.py
52 lines (51 loc) · 1.09 KB
/
p28.py
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
44
45
46
47
48
49
50
51
52
from itertools import count
# right 1, down one, left two, up two,
# right three, down three, left four, up four
# right five, down five, left six, up six
# make the matrix
SIZE=1001
#matrix = []
#for i in range(0,SIZE):
# matrix.append([1]*SIZE)
x,y = (SIZE/2,SIZE/2) #center
def down():
i = 1
while 1:
yield (2*i-1)
i += 1
right, down = down().next, down().next
def up():
i = 1
while 1:
yield 2*i
i += 1
left, up = up().next, up().next
current = count(2).next
tot = 1
done = False
def match(x,y):
return x == y or (SIZE-x-1 == y)
while not done:
for i in range(right()):
c = current()
x += 1
if (y,x) == (0, SIZE):
done = True
break
if match(x,y):
tot += c
if done: break
for i in range(down()):
y += 1
c = current()
if match(x,y):
tot += c
for i in range(left()):
x -= 1
c = current()
if match(x,y): tot += c
for i in range(up()):
y -= 1
c = current()
if match(x,y): tot +=c
print tot