-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday15.py
More file actions
63 lines (40 loc) · 1.28 KB
/
day15.py
File metadata and controls
63 lines (40 loc) · 1.28 KB
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
# Advent of Code 2021, Day 15
# markusremplbauer
from itertools import product
import networkx as nx
import numpy as np
from aocd.models import Puzzle
from funcy import print_calls
@print_calls
def part1(data):
return solve(data)
@print_calls
def part2(data):
return solve(extend_grid(data, 5))
def solve(data):
graph = nx.grid_2d_graph(*data.shape)
end = data.shape[0] - 1, data.shape[1] - 1
p = nx.shortest_path(
graph, source=(0, 0), target=end, weight=lambda u, v, d: data[v]
)
return sum(data[(x, y)] for x, y in p[1:])
def extend_grid(tile, n):
h, w = tile.shape
grid = np.zeros((h * n, w * n), dtype=int)
# iterate over all n*n tiles
for x_i, y_i in product(range(n), repeat=2):
# iterate over the values of the act tile
for x, y in np.ndindex(tile.shape):
v = (tile[x, y] - 1 + x_i + y_i) % 9 + 1
grid[x + x_i * h, y + y_i * w] = v
return grid
def load(data):
return np.fromiter(data.replace("\n", ""), dtype=int).reshape(-1, data.index("\n"))
def main():
puzzle = Puzzle(year=2021, day=15)
ans1 = part1(load(puzzle.input_data))
# puzzle.answer_a = ans1
ans2 = part2(load(puzzle.input_data))
# puzzle.answer_b = ans2
if __name__ == "__main__":
main()