-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday16.py
54 lines (35 loc) · 1.09 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
from collections import defaultdict
data = open(0).read().splitlines()
maze = {}
for y, line in enumerate(data):
for x, c in enumerate(line):
if c == "S":
start = (x, y)
if c == "E":
end = (x, y)
maze[x, y] = c
def bfs():
q = [([(start[0] - 1, start[1])], start, 0)]
dist = defaultdict(lambda: float("inf"))
paths = defaultdict(set)
while q:
hist, (x, y), d = q.pop(0)
(px, py) = hist[-1]
if (x, y) == end:
paths[d].update(hist)
for dx, dy in (-1, 0), (1, 0), (0, -1), (0, 1):
nx, ny = x + dx, y + dy
if (nx, ny) == (px, py):
continue
newd = d + (1 if (x - px == dx) and (y - py == dy) else 1001)
if maze[nx, ny] != "#" and newd < dist[nx, ny] + 1001:
dist[nx, ny] = min(newd, dist[nx, ny])
q.append((hist + [(x, y)], (nx, ny), newd))
return dist, paths
dist, paths = bfs()
def part1():
return dist[end]
def part2():
return len(paths[dist[end]])
print(part1())
print(part2())