-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.py
39 lines (34 loc) · 876 Bytes
/
13.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
from lib import *
input = read_input(2016, 13)
num = int(input)
wall = lambda x, y: bin(x * x + 3 * x + 2 * x * y + y + y * y + num).count("1") % 2
q = [(0, 1, 1)]
visited = set()
while q:
d, x, y = q.pop(0)
if (x, y) in visited:
continue
visited.add((x, y))
if (x, y) == (31, 39):
print(d)
break
for nx, ny in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]:
if nx < 0 or ny < 0 or wall(nx, ny):
continue
q.append((d + 1, nx, ny))
q = [(0, 1, 1)]
visited = set()
count = 0
while q:
d, x, y = q.pop(0)
if (x, y) in visited:
continue
visited.add((x, y))
if d > 50:
break
count += 1
for nx, ny in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]:
if nx < 0 or ny < 0 or wall(nx, ny):
continue
q.append((d + 1, nx, ny))
print(count)