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
|
def move_on_trail(map_positions: list[list[int]], current_row: int, current_col: int, next_height: int) -> int:
summits_found = 0
row_count = len(map_positions)
col_count = len(map_positions[0])
for dir_row, dir_col in ((-1, 0), (1, 0), (0, -1), (0, 1)):
next_row = current_row + dir_row
next_col = current_col + dir_col
if not (0 <= next_row < row_count and 0 <= next_col < col_count):
continue
if map_positions[next_row][next_col] == next_height:
if next_height == 9:
summits_found += 1
else:
summits_found += move_on_trail(map_positions, next_row, next_col, next_height + 1)
return summits_found
def get_trailhead_rating(map_positions: list[list[int]], head_row: int, head_col: int) -> int:
return move_on_trail(map_positions, head_row, head_col, 1)
data = []
with open("input") as f:
for line in f:
data.append([int(elem) for elem in tuple(line.strip())])
count = 0
for row, line in enumerate(data):
for col, height in enumerate(line):
if height == 0:
count += get_trailhead_rating(data, row, col)
print(count)
|