-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3b_aoc.py
48 lines (41 loc) · 892 Bytes
/
3b_aoc.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
from itertools import *
num = int(raw_input())
def up(x,y):
return (x,y+1)
def left(x,y):
return (x-1,y)
def right(x,y):
return (x+1,y)
def down(x,y):
return (x,y-1)
def sum_neighbours(x,y,grid):
neighbours = list(product([-1,0,1],[-1,0,1]))
neighbours.remove((0,0))
s = 0
for (i,j) in neighbours:
try:
s += grid[(x+i,y+j)]
except KeyError:
pass
return s
directions = [up,left,down,right]
# up = 0, left = 1, down = 2, right = 3
going = 3
steps_to_take = 1
steps_taken = 0
x,y = 0,0
j = 1
grid = {(0,0):1}
while j<num:
f = directions[going]
x,y = f(x,y)
j = sum_neighbours(x,y,grid)
grid[(x,y)] = j
steps_taken += 1
if steps_taken==steps_to_take:
going += 1
going %= 4
steps_taken = 0
if going == 1 or going == 3:
steps_to_take += 1
print j