From 3b1b3fe99e6953b53a44c070d227ad36341e71c4 Mon Sep 17 00:00:00 2001 From: Botond Hende Date: Wed, 11 Dec 2024 10:20:30 +0100 Subject: 2024 day9-10 --- 2024/day10/input | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2024/day10/input_test | 8 ++++++++ 2024/day10/solve.py | 35 +++++++++++++++++++++++++++++++++++ 2024/day10/solve2.py | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+) create mode 100644 2024/day10/input create mode 100644 2024/day10/input_test create mode 100644 2024/day10/solve.py create mode 100644 2024/day10/solve2.py (limited to '2024/day10') diff --git a/2024/day10/input b/2024/day10/input new file mode 100644 index 0000000..02b092c --- /dev/null +++ b/2024/day10/input @@ -0,0 +1,45 @@ +012589824304345454345653403898787654310365890 +323456712215216783238732312345693201223456781 +450144603304306690129641002105892100378901672 +343223454456987601234553213456743898465432543 +234019765567898501246764789122156784567643456 +105678876678983454334895601033021093298932167 +987619989569332569543210592344534580112341098 +294501238410221078632346787659645676903654348 +103410549322101010701458698998765019854789239 +212321678989230127890569542345674326761021178 +101436769476541436909878431089781015432430087 +212345854300498545415678921678196727034567696 +210256901211067636324105670543067878123678545 +367107810562109897013234787102158969243989430 +458998765673456708943101496234549054552170121 +321043234589843217652112345109632143461078760 +423450103477876876501001232238761232678989654 +214567612100945983492376521045690101021234523 +105678998761238012385489431058788987120098012 +210987787687439121076378942389005678967127656 +309456712596528732154307875470114343458934565 +498349803487015678567210566965223062101703403 +567208701236012569458923417854332176019812212 +610119654345643454345698706763894385498703389 +231023543210789843210789210012765294307654471 +132984980125632310107650123498874101212343560 +045675670346541001238943210567943079810014541 +125036561257892890547765300343012986701321632 +034123498766783087654854301250123875432430701 +945671210345892167898901232765234566001521894 +876980343210763012789876501875678987189654323 +567878432198754803650105432984789843278789010 +438969567087634984543215678793212658943898123 +821054698456125675643454349783104367652387654 +981023789307087676012067245692323456701098578 +672316548218996587654198130541214987898123469 +543007437100105498903210021230301854302210158 +432108128901234567810347130765415675211000147 +543219037654323216921438941891034986732789230 +698349898341010105434567234567127867845654321 +786458723038765123457870123898221058965421032 +665467012129851034566962123765432348974330541 +674398543122345643677870054878901067889245670 +983287654031298762986521067965013454900196989 +210189876540107601095432398874322103210187878 \ No newline at end of file diff --git a/2024/day10/input_test b/2024/day10/input_test new file mode 100644 index 0000000..40a0591 --- /dev/null +++ b/2024/day10/input_test @@ -0,0 +1,8 @@ +89010123 +78121874 +87430965 +96549874 +45678903 +32019012 +01329801 +10456732 \ No newline at end of file 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) diff --git a/2024/day10/solve2.py b/2024/day10/solve2.py new file mode 100644 index 0000000..df633ad --- /dev/null +++ b/2024/day10/solve2.py @@ -0,0 +1,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) -- cgit v1.2.3-70-g09d2