-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_10.py
68 lines (52 loc) · 1.94 KB
/
day_10.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
from typing import Callable
type Map = list[list[int]]
type Position = tuple[int, int]
def read_map() -> Map:
with open('./inputs/day_10.txt') as f:
return [
[int(height) for height in line.rstrip()]
for line in f
]
def follow_trail(map: Map, position: Position, previous_height: int, on_trail_end: Callable):
x, y = position
if x < 0 or x >= len(map) or y < 0 or y >= len(map[0]):
return
current_height = map[x][y]
if current_height - previous_height != 1:
return
elif current_height == 9:
on_trail_end(position)
return
follow_trail(map, (x, y+1), current_height, on_trail_end)
follow_trail(map, (x, y-1), current_height, on_trail_end)
follow_trail(map, (x-1, y), current_height, on_trail_end)
follow_trail(map, (x+1, y), current_height, on_trail_end)
return
def evaluate_trailhead_score(map: Map, trailhead_position: Position) -> int:
found_trail_ends = dict()
def on_trail_end(p):
found_trail_ends[f"{p[0],p[1]}"] = True
follow_trail(map, trailhead_position, -1, on_trail_end)
return len(found_trail_ends)
def evaluate_trailhead_rating(map: Map, trailhead_position: Position) -> int:
rating = 0
def on_trail_end(p):
nonlocal rating
rating += 1
follow_trail(map, trailhead_position, -1, on_trail_end)
return rating
def get_total_trailhead_point(map: Map, evaluation_function: Callable) -> int:
return sum(
evaluation_function(map, (x, y))
for x in range(len(map))
for y in range(len(map[0]))
if map[x][y] == 0
)
def solve_part_1(map: Map):
return get_total_trailhead_point(map, evaluate_trailhead_score)
def solve_part_2(map: Map):
return get_total_trailhead_point(map, evaluate_trailhead_rating)
if __name__ == '__main__':
map = read_map()
print(f"Answer for part 1: {solve_part_1(map)}")
print(f"Answer for part 1: {solve_part_2(map)}")