-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday16.py
executable file
·69 lines (51 loc) · 1.26 KB
/
day16.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
#!/usr/bin/env python3
from utils.all import *
advent.setup(2024, 16)
fin = advent.get_input()
grid = read_char_matrix(fin)
ans1 = ans2 = 0
g = defaultdict(list)
for r, row in enumerate(grid):
for c, char in enumerate(row):
if char == 'S':
start = Vec(r, c)
if char == 'E':
end = Vec(r, c)
for nr, nc in neighbors4(grid, r, c):
if grid[nr][nc] != '#':
g[Vec(r,c)].append(Vec(nr, nc))
def angle(a, b):
d = b - a
if abs(d.r) == 2 or abs(d.c) == 2:
return 2000
if abs(d.r) == 1 or abs(d.c) == 1:
return 1000
return 0
def explore(src, dst, face):
q = [(0, src, face, frozenset([src]))]
distance = defaultdict(lambda: INFINITY)
best_path_points = set()
best = INFINITY
while q:
score, pos, face, path = heapq.heappop(q)
if pos == dst:
if score < best:
best = score
best_path_points = path
elif score == best:
best_path_points |= path
continue
k = pos, face
if distance[k] < score:
continue
distance[k] = score
for n in g[pos]:
if n in path:
continue
direction = n - pos
a = angle(face, direction)
heapq.heappush(q, (score + a + 1, n, direction, path | {n}))
return best, len(best_path_points)
ans1, ans2 = explore(start, end, Vec(0, 1))
advent.print_answer(1, ans1)
advent.print_answer(2, ans2)