-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.py
50 lines (34 loc) · 1.17 KB
/
day10.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
import sys
from pathlib import Path
import numpy as np
def parse_input(s: str):
return np.array([[int(s) for s in line] for line in s.splitlines()])
def get_trailheads(a: np.ndarray):
return np.argwhere(a == 0)
def paths(a: np.ndarray, y: int, x: int):
def in_field(y, x) -> bool:
return 0 <= y < a.shape[0] and 0 <= x < a.shape[1]
assert a[y][x] == 0
queue = [[(y, x)]]
valid_paths = []
while queue:
path = queue.pop()
y, x = path[0]
height = a[y][x]
if height == 9:
valid_paths.append(path)
else:
for y2, x2 in ((y - 1, x), (y, x - 1), (y + 1, x), (y, x + 1)):
if in_field(y2, x2) and (a[y2][x2] == height + 1):
queue.append([(y2, x2), *path])
return valid_paths
def part1(map: np.ndarray):
return sum([len({path[0] for path in paths(map, *th)}) for th in get_trailheads(map)])
def part2(map: np.ndarray):
return sum([len(paths(map, *th)) for th in get_trailheads(map)])
def main():
m = parse_input(Path(sys.argv[1]).read_text())
print(f"Part1: {part1(m)}")
print(f"Part2: {part2(m)}")
if __name__ == "__main__":
main()