-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.py
35 lines (28 loc) · 897 Bytes
/
day13.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
FAVORITE_NUMBER = 1362
TARGET = (31, 39)
def is_open(x, y):
return (x*x + 3*x + 2*x*y + y + y*y + FAVORITE_NUMBER).bit_count() % 2 == 0
start = (1, 1)
Q = [start]
seen = set(Q)
steps = 0
target_steps = None
reachable_in_50 = None
while target_steps is None or reachable_in_50 is None:
next_Q = []
for x, y in Q:
if (x, y) == TARGET:
target_steps = steps
left, right, up, down = (x-1, y), (x+1, y), (x, y-1), (x, y+1)
for neighbor in (left, right, up, down):
if x == 0 and neighbor == left or y == 0 and neighbor == up:
continue
if is_open(*neighbor) and neighbor not in seen:
next_Q.append(neighbor)
seen.add(neighbor)
Q = next_Q
steps += 1
if steps == 50:
reachable_in_50 = len(seen)
print('part 1:', target_steps)
print('part 2:', reachable_in_50)