summaryrefslogtreecommitdiff
path: root/2024/day10/solve.py
diff options
context:
space:
mode:
authorBotond Hende <nettingman@gmail.com>2024-12-11 10:20:30 +0100
committerBotond Hende <nettingman@gmail.com>2024-12-11 10:20:30 +0100
commit3b1b3fe99e6953b53a44c070d227ad36341e71c4 (patch)
treedc6d94cbbe54997568777a208d8e15c074d38529 /2024/day10/solve.py
parent0a5c1f77d01fc0bd166494787f24562e2fd3a9e9 (diff)
2024 day9-10
Diffstat (limited to '2024/day10/solve.py')
-rw-r--r--2024/day10/solve.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/2024/day10/solve.py b/2024/day10/solve.py
new file mode 100644
index 0000000..235fbdf
--- /dev/null
+++ b/2024/day10/solve.py
@@ -0,0 +1,35 @@
+def move_on_trail(summits_found: set[tuple[int, int]], map_positions: list[list[int]], current_row: int, current_col: int, next_height: int) -> None:
+ 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.add((next_row, next_col))
+ else:
+ move_on_trail(summits_found, map_positions, next_row, next_col, next_height + 1)
+
+
+def get_trailhead_score(map_positions: list[list[int]], head_row: int, head_col: int) -> int:
+ summits_reached = set()
+ move_on_trail(summits_reached, map_positions, head_row, head_col, 1)
+ return len(summits_reached)
+
+
+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_score(data, row, col)
+
+print(count)