-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15.py
52 lines (38 loc) · 1.31 KB
/
day15.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
import numpy as np
from queue import PriorityQueue
data = open("input.txt").read().strip().splitlines()
b = np.array([list(map(int, [c for c in line])) for line in data])
w, h = b.shape
START, END = (0, 0), (w - 1, h - 1)
def get_neigbors(v):
vx, vy = v
out = []
for dx, dy in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
nx, ny = vx + dx, vy + dy
if nx < 0 or nx >= w or ny < 0 or ny >= h: continue
out.append((nx, ny))
return out
def dij(matrix, start_v, end_v):
D = np.full(matrix.shape, 10 ** 9, dtype=int)
D[start_v] = 0
visited = set()
pq = PriorityQueue()
pq.put((0, start_v))
while not pq.empty():
dist, current_v = pq.get()
visited.add(current_v)
for neighbor in get_neigbors(current_v):
if neighbor in visited: continue
new_cost = D[current_v] + matrix[neighbor]
if new_cost < D[neighbor]:
pq.put((new_cost, neighbor))
D[neighbor] = new_cost
return D[end_v]
# one star
print(dij(b, START, END))
# two stars
row = np.hstack([(b + i - 1) % 9 + 1 for i in range(5)])
big_map = np.vstack([(row + i - 1) % 9 + 1 for i in range(5)])
w, h = big_map.shape
START, END = (0, 0), (w - 1, h - 1)
print(dij(big_map, START, END))