-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_13.py
70 lines (51 loc) · 1.66 KB
/
day_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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# https://adventofcode.com/2016/day/13
from helper import get_input, print_result
DAY = 13
inp = get_input(DAY)
START = (1, 1)
END = (31, 39)
FAV_NUM = int(inp[0])
DIR = [(0, 1), (1, 0), (-1, 0), (0, -1)]
def is_empty(location):
# Is given location empty?
x, y = location
magic_num = (x * x) + (3 * x) + (2 * x * y) + y + (y * y) + FAV_NUM
ones = magic_num.bit_count()
return not ones % 2
def p1():
queue = [] # ((x,y), dist)
visited = set()
queue.append((START, 0))
visited.add(START)
while queue:
cur, dist = queue.pop(0)
dist += 1
for d in DIR:
new_point = (cur[0] + d[0], cur[1] + d[1])
_x, _y = new_point
if _x >= 0 and _y >= 0 and new_point not in visited and is_empty(new_point):
if new_point == END:
# Found the END!
return dist
queue.append((new_point, dist))
visited.add(new_point)
return -1
def p2():
queue = [] # ((x,y), dist)
visited = set()
queue.append((START, 0))
visited.add(START)
while queue:
cur, dist = queue.pop(0)
dist += 1
for d in DIR:
new_point = (cur[0] + d[0], cur[1] + d[1])
_x, _y = new_point
if _x >= 0 and _y >= 0 and new_point not in visited and is_empty(new_point):
if dist <= 50:
# add in queue only if dist is less than 50
# i.e. no need to visit >50 dist points
queue.append((new_point, dist))
visited.add(new_point)
return len(visited)
print_result(DAY, p1(), p2())