From 96f139fe100f22e86b245e76d9b251ce4a1e2edc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Fri, 20 Dec 2024 09:19:12 +0100 Subject: [PATCH] 2024 day 20. --- src/holt59/aoc/2024/day20.py | 92 ++++++++++++- src/holt59/aoc/inputs/holt59/2024/day20.txt | 141 ++++++++++++++++++++ src/holt59/aoc/inputs/tests/2024/day20.txt | 15 +++ src/holt59/aoc/tools/graphs.py | 27 +++- 4 files changed, 271 insertions(+), 4 deletions(-) diff --git a/src/holt59/aoc/2024/day20.py b/src/holt59/aoc/2024/day20.py index 07e201e..d42e37c 100644 --- a/src/holt59/aoc/2024/day20.py +++ b/src/holt59/aoc/2024/day20.py @@ -1,7 +1,95 @@ -from typing import Any, Iterator +import itertools +from collections import Counter +from typing import Any, Callable, Iterable, Iterator, Sequence, TypeAlias from ..base import BaseSolver +from ..tools.graphs import dijkstra, make_neighbors_grid_fn + +Node: TypeAlias = tuple[int, int] + + +def make_neighbors_fn(grid: list[str], cheat_length: int): + n_rows, n_cols = len(grid), len(grid[0]) + + def _fn(node: Node): + row, col = node + return ( + ((row_n, col_n), abs(row_n - row) + abs(col_n - col)) + for row_d in range(-cheat_length, cheat_length + 1) + for col_d in range( + -cheat_length + abs(row_d), cheat_length - abs(row_d) + 1 + ) + if 0 <= (row_n := row + row_d) < n_rows + and 0 <= (col_n := col + col_d) < n_cols + and grid[row_n][col_n] != "#" + ) + + return _fn class Solver(BaseSolver): - def solve(self, input: str) -> Iterator[Any]: ... + def find_cheats( + self, + path: Sequence[Node], + cost: float, + costs_to_target: dict[Node, float], + neighbors_fn: Callable[[Node], Iterable[tuple[Node, float]]], + ): + cheats: dict[tuple[tuple[int, int], tuple[int, int]], float] = {} + + for i_node, node in enumerate(self.progress.wrap(path)): + for reach_node, reach_cost in neighbors_fn(node): + n_cost = ( + i_node + reach_cost + costs_to_target.get(reach_node, float("inf")) + ) + if n_cost < cost: + cheats[node, reach_node] = cost - n_cost + + return cheats + + def solve(self, input: str) -> Iterator[Any]: + grid = input.splitlines() + n_rows, n_cols = len(grid), len(grid[0]) + start = next( + (i, j) for i in range(n_rows) for j in range(n_cols) if grid[i][j] == "S" + ) + target = next( + (i, j) for i in range(n_rows) for j in range(n_cols) if grid[i][j] == "E" + ) + + reachable = dijkstra( + target, + None, + make_neighbors_grid_fn( + n_rows, + n_cols, + excluded=( + (i, j) + for i in range(n_rows) + for j in range(n_cols) + if grid[i][j] == "#" + ), + ), + ) + + # note: path is inverted here + path, cost = reachable[start] + costs_to_target = {k: c for k, (_, c) in reachable.items()} + + self.logger.info(f"found past from start to target with cost {cost}") + + for cheat_length in (2, 20): + cheats = self.find_cheats( + list(reversed(path)), + cost, + costs_to_target, + make_neighbors_fn(grid, cheat_length), + ) + + for saving, count in sorted(Counter(cheats.values()).items()): + self.logger.debug( + f"There are {count} cheats that save {saving} picoseconds." + ) + + target_saving = 100 if len(grid) > 20 else 50 + yield sum(saving >= target_saving for saving in cheats.values()) diff --git a/src/holt59/aoc/inputs/holt59/2024/day20.txt b/src/holt59/aoc/inputs/holt59/2024/day20.txt index e69de29..03bb6ca 100644 --- a/src/holt59/aoc/inputs/holt59/2024/day20.txt +++ b/src/holt59/aoc/inputs/holt59/2024/day20.txt @@ -0,0 +1,141 @@ +############################################################################################################################################# +#.......#.........#.....#.....#...#...###...###.....###...#...###.....#.....#.......#.......#.....#...#####.....#...#...#...#...............# +#.#####.#.#######.#.###.#.###.#.#.#.#.###.#.###.###.###.#.#.#.###.###.#.###.#.#####.#.#####.#.###.#.#.#####.###.#.#.#.#.#.#.#.#############.# +#...#...#.......#.#...#.#...#.#.#...#...#.#...#...#.....#.#.#...#...#.#...#.#.....#.#...#...#.#...#.#...#...#...#.#...#.#.#.#...#...........# +###.#.#########.#.###.#.###.#.#.#######.#.###.###.#######.#.###.###.#.###.#.#####.#.###.#.###.#.###.###.#.###.###.#####.#.#.###.#.########### +#...#.....#.....#.#...#.#...#.#.......#.#.#...###.#.......#.#...#...#.....#.......#...#.#...#.#...#.#...#...#.###.....#...#...#.#...........# +#.#######.#.#####.#.###.#.###.#######.#.#.#.#####.#.#######.#.###.###################.#.###.#.###.#.#.#####.#.#######.#######.#.###########.# +#...#.....#.....#.#.#...#...#...#...#.#...#...#...#.......#.#...#...........#.......#...#...#.#...#.#...#...#...#...#.#.......#.#.........#.# +###.#.#########.#.#.#.#####.###.#.#.#.#######.#.#########.#.###.###########.#.#####.#####.###.#.###.###.#.#####.#.#.#.#.#######.#.#######.#.# +###.#.###.....#.#.#.#.#...#.#...#.#.#...#.....#.....#.....#.#...#...###...#...#...#.#.....#...#...#.#...#...#...#.#.#.#.###...#.#.#.....#.#.# +###.#.###.###.#.#.#.#.#.#.#.#.###.#.###.#.#########.#.#####.#.###.#.###.#.#####.#.#.#.#####.#####.#.#.#####.#.###.#.#.#.###.#.#.#.#.###.#.#.# +#...#...#...#.#.#.#.#.#.#...#.#...#.....#...###.....#...#...#...#.#.#...#.###...#...#...###.....#.#.#.#.....#...#.#.#.#.....#...#...#...#...# +#.#####.###.#.#.#.#.#.#.#####.#.###########.###.#######.#.#####.#.#.#.###.###.#########.#######.#.#.#.#.#######.#.#.#.###############.####### +#.....#.....#...#.#.#.#.....#.#...........#...#.#.....#.#.....#.#.#.#.#...#...#.......#.#...###.#.#.#.#.....#...#.#...#.............#.......# +#####.###########.#.#.#####.#.###########.###.#.#.###.#.#####.#.#.#.#.#.###.###.#####.#.#.#.###.#.#.#.#####.#.###.#####.###########.#######.# +#...#.........#...#.#.#...#.#...#...#...#.#...#.#...#...#.....#.#.#...#.###...#.#.....#...#...#.#.#.#.....#.#.###.#...#...........#...#.....# +#.#.#########.#.###.#.#.#.#.###.#.#.#.#.#.#.###.###.#####.#####.#.#####.#####.#.#.###########.#.#.#.#####.#.#.###.#.#.###########.###.#.##### +#.#...........#...#.#.#.#.#...#...#.#.#.#.#.....#...#...#...#...#...#...#...#...#...#.....#...#.#...#.....#.#...#.#.#.......#...#...#.#.....# +#.###############.#.#.#.#.###.#####.#.#.#.#######.###.#.###.#.#####.#.###.#.#######.#.###.#.###.#####.#####.###.#.#.#######.#.#.###.#.#####.# +#...#.....#.....#...#...#...#.....#.#.#.#.#.......#...#.....#...#...#...#.#...#...#.#...#...###.....#.#...#...#.#.#.#...###.#.#.#...#.......# +###.#.###.#.###.###########.#####.#.#.#.#.#.#######.###########.#.#####.#.###.#.#.#.###.###########.#.#.#.###.#.#.#.#.#.###.#.#.#.########### +###...###...#...#...#...#...#...#.#.#.#.#.#...#...#.........#...#.....#.#.#...#.#...#...#...#...#...#...#.....#...#...#...#.#.#...#...#...### +#############.###.#.#.#.#.###.#.#.#.#.#.#.###.#.#.#########.#.#######.#.#.#.###.#####.###.#.#.#.#.#######################.#.#.#####.#.#.#.### +#.............#...#...#.#...#.#.#.#.#.#.#.#...#.#.#...###...#.#...#...#...#...#...#...#...#.#.#.#.....#.........#.........#.#.....#.#.#.#...# +#.#############.#######.###.#.#.#.#.#.#.#.#.###.#.#.#.###.###.#.#.#.#########.###.#.###.###.#.#.#####.#.#######.#.#########.#####.#.#.#.###.# +#.#.............###...#...#.#.#...#...#...#.....#.#.#.#...#...#.#.#...#.......#...#...#.#...#.#.#...#.#.......#.#.......###.......#.#.#.#...# +#.#.###############.#.###.#.#.###################.#.#.#.###.###.#.###.#.#######.#####.#.#.###.#.#.#.#.#######.#.#######.###########.#.#.#.### +#...#...#...........#.....#...#...#...#...#.......#.#.#...#...#.#.#...#.#.....#.....#.#.#...#.#.#.#...#.......#...#...#.......#.....#...#...# +#####.#.#.#####################.#.#.#.#.#.#.#######.#.###.###.#.#.#.###.#.###.#####.#.#.###.#.#.#.#####.#########.#.#.#######.#.###########.# +#...#.#.#.........#.......#.....#...#...#.#.#...###.#.#...#...#.#.#...#...#...#.....#.#...#.#.#.#.#...#...###...#...#.........#...#.....#...# +#.#.#.#.#########.#.#####.#.#############.#.#.#.###.#.#.###.###.#.###.#####.###.#####.###.#.#.#.#.#.#.###.###.#.#################.#.###.#.### +#.#...#.......#...#.#.....#...#...#...#...#...#.#...#.#...#...#.#...#.#...#...#.....#...#.#...#.#...#.#...#...#.......###...#...#.#...#.#...# +#.###########.#.###.#.#######.#.#.#.#.#.#######.#.###.###.###.#.###.#.#.#.###.#####.###.#.#####.#####.#.###.#########.###.#.#.#.#.###.#.###.# +#.........#...#.....#.........#.#...#.#.#.......#...#.###.#...#.#...#...#.#...#...#.#...#.#.....#...#.#.###.#.......#.#...#...#.#.#...#.....# +#########.#.###################.#####.#.#.#########.#.###.#.###.#.#######.#.###.#.#.#.###.#.#####.#.#.#.###.#.#####.#.#.#######.#.#.######### +###...###.#.#...#...#.........#.....#...#...#.....#.#.#...#.#...#...#...#.#.....#.#.#.#...#...#...#...#.....#.....#.#.#...#.....#.#.....#...# +###.#.###.#.#.#.#.#.#.#######.#####.#######.#.###.#.#.#.###.#.#####.#.#.#.#######.#.#.#.#####.#.#################.#.#.###.#.#####.#####.#.#.# +#...#.....#...#.#.#.#.....#...#...#.......#.#.#...#.#.#...#.#.....#.#.#.#.....###.#.#.#.#.....#.....###.........#.#...###.#.......#...#...#.# +#.#############.#.#.#####.#.###.#.#######.#.#.#.###.#.###.#.#####.#.#.#.#####.###.#.#.#.#.#########.###.#######.#.#######.#########.#.#####.# +#.#...........#...#.......#.#...#.#.......#.#.#...#.#.#...#.#.....#.#.#.#...#.#...#.#.#.#...#.....#...#.......#...#.......#.........#.....#.# +#.#.#########.#############.#.###.#.#######.#.###.#.#.#.###.#.#####.#.#.#.#.#.#.###.#.#.###.#.###.###.#######.#####.#######.#############.#.# +#...#.....###.............#.#...#...#.....#...###.#.#.#.#...#...#...#.#.#.#.#.#...#.#...#...#.###.#...#.....#...#...#...#...#...#...#...#...# +#####.###.###############.#.###.#####.###.#######.#.#.#.#.#####.#.###.#.#.#.#.###.#.#####.###.###.#.###.###.###.#.###.#.#.###.#.#.#.#.#.##### +#...#...#.....#...........#.....#...#.#...#.....#...#...#...#...#...#.#.#.#.#S###.#.....#...#...#...###...#...#...#...#.#.....#.#.#...#...### +#.#.###.#####.#.#################.#.#.#.###.###.###########.#.#####.#.#.#.#.#####.#####.###.###.#########.###.#####.###.#######.#.#######.### +#.#.###.....#...#...#.....#.......#...#...#.#...#...#.......#.#...#...#...#...###.#.....#...#...#...#...#...#.#...#...#.........#.......#...# +#.#.#######.#####.#.#.###.#.#############.#.#.###.#.#.#######.#.#.###########.###.#.#####.###.###.#.#.#.###.#.#.#.###.#################.###.# +#.#.#...#...#...#.#.#.###.#.###.........#...#.....#.#.#.....#...#.......#.....###.#...#...#...###.#...#.....#...#.....#.....#...#.......#...# +#.#.#.#.#.###.#.#.#.#.###.#.###.#######.###########.#.#.###.###########.#.#######.###.#.###.#####.#####################.###.#.#.#.#######.### +#.#.#.#.#...#.#...#...#...#.#...#.....#.#...#...#...#.#.#...#.....#...#.#.#...###.#...#...#.....#...#...........###...#...#.#.#.#.#.......### +#.#.#.#.###.#.#########.###.#.###.###.#.#.#.#.#.#.###.#.#.###.###.#.#.#.#.#.#.###.#.#####.#####.###.#.#########.###.#.###.#.#.#.#.#.######### +#.#...#...#...###...###...#.#.....#...#.#.#...#...###...#...#...#.#.#...#...#.###.#.#.....#...#.#...#.#.........#...#...#.#...#...#...#...### +#.#######.#######.#.#####.#.#######.###.#.#################.###.#.#.#########.###.#.#.#####.#.#.#.###.#.#########.#####.#.###########.#.#.### +#.......#.....#...#.#.....#.#.......###...#.........#.......#...#...###...#...###...#...#...#...#.....#...........#...#.#.#...........#.#...# +#######.#####.#.###.#.#####.#.#############.#######.#.#######.#########.#.#.###########.#.#########################.#.#.#.#.###########.###.# +#...###.#.....#...#...#...#.#...#...#.....#.#.......#.....#...###...#...#.#...#######...#.###.................#...#.#...#.#...#...#.....#...# +#.#.###.#.#######.#####.#.#.###.#.#.#.###.#.#.###########.#.#####.#.#.###.###.#######.###.###.###############.#.#.#.#####.###.#.#.#.#####.### +#.#.....#.....###...#...#...###...#...#...#.#.....###...#.#...#...#.#...#.....#######...#.#...#.......#.....#.#.#...#...#.#...#.#...#...#...# +#.###########.#####.#.#################.###.#####.###.#.#.###.#.###.###.###############.#.#.###.#####.#.###.#.#.#####.#.#.#.###.#####.#.###.# +#...#.......#.#...#...###...#...#.......###.#.....#...#.#.....#...#.#...#.....#######...#.#...#.#.....#.#...#...#.....#...#.....#.....#.....# +###.#.#####.#.#.#.#######.#.#.#.#.#########.#.#####.###.#########.#.#.###.###.#######.###.###.#.#.#####.#.#######.###############.########### +#...#.#...#.#.#.#.........#.#.#.#.......#...#.....#...#.#...#...#.#...#...#...#######...#.###...#.......#.........#.......#.....#...........# +#.###.#.#.#.#.#.###########.#.#.#######.#.#######.###.#.#.#.#.#.#.#####.###.###########.#.#########################.#####.#.###.###########.# +#.#...#.#...#...#.........#...#.........#.#.....#...#.#.#.#.#.#.#.#...#...#...#...###E#...#...###...........#.....#.....#.#...#.#.....#.....# +#.#.###.#########.#######.###############.#.###.###.#.#.#.#.#.#.#.#.#.###.###.#.#.###.#####.#.###.#########.#.###.#####.#.###.#.#.###.#.##### +#...###.....#...#.#...###.................#.###.....#.#...#.#.#.#...#.#...#...#.#.#...#.....#.#...#...#...#...###.......#.#...#.#...#.#.....# +###########.#.#.#.#.#.#####################.#########.#####.#.#.#####.#.###.###.#.#.###.#####.#.###.#.#.#.###############.#.###.###.#.#####.# +#...###...#...#...#.#.#...#...#...#...#...#...#.......#...#...#.....#...###...#.#...###.....#.#...#.#...#.......#.......#.#...#...#.#...#...# +#.#.###.#.#########.#.#.#.#.#.#.#.#.#.#.#.###.#.#######.#.#########.#########.#.###########.#.###.#.###########.#.#####.#.###.###.#.###.#.### +#.#.#...#...#...#...#...#...#...#...#...#...#...#.....#.#.........#.#.....###...#.....#...#.#.###...#...........#.#.....#.....#...#...#...### +#.#.#.#####.#.#.#.#########################.#####.###.#.#########.#.#.###.#######.###.#.#.#.#.#######.###########.#.###########.#####.####### +#.#.#.....#.#.#.#.......................###.#.....###.#.........#...#...#.......#...#.#.#...#.#...###.............#.....###...#...#...#...### +#.#.#####.#.#.#.#######################.###.#.#######.#########.#######.#######.###.#.#.#####.#.#.#####################.###.#.###.#.###.#.### +#.#.#...#.#...#.......................#...#...#.....#...........#...#...#.......#...#.#.....#.#.#.#...###.....#.......#.....#...#...#...#...# +#.#.#.#.#.###########################.###.#####.###.#############.#.#.###.#######.###.#####.#.#.#.#.#.###.###.#.#####.#########.#####.#####.# +#.#...#...#...#.......#.....#...#...#.....#...#.#...###...#...###.#.#...#.....###...#.###...#...#...#.....###.#.....#...#...#...#...#...#...# +#.#########.#.#.#####.#.###.#.#.#.#.#######.#.#.#.#####.#.#.#.###.#.###.#####.#####.#.###.###################.#####.###.#.#.#.###.#.###.#.### +#...#.....#.#.#.#.....#.#...#.#...#.....###.#.#.#.......#.#.#.#...#...#.#.....#...#.#.....#.....#...#.......#.#...#.###...#...#...#.....#...# +###.#.###.#.#.#.#.#####.#.###.#########.###.#.#.#########.#.#.#.#####.#.#.#####.#.#.#######.###.#.#.#.#####.#.#.#.#.###########.###########.# +#...#...#.#.#.#.#...#...#...#.#.........#...#...#...#.....#.#.#.....#.#.#.#...#.#.#.......#...#...#.#.#.....#...#.#.#...#...#...#...#.......# +#.#####.#.#.#.#.###.#.#####.#.#.#########.#######.#.#.#####.#.#####.#.#.#.#.#.#.#.#######.###.#####.#.#.#########.#.#.#.#.#.#.###.#.#.####### +#.......#...#...###...#####...#...........#...#...#...###...#...###.#.#.#.#.#...#...#.....#...#...#...#...#...###.#...#...#...#...#.#.......# +###########################################.#.#.#########.#####.###.#.#.#.#.#######.#.#####.###.#.#######.#.#.###.#############.###.#######.# +#.........#.....###.....#.......#.......#...#.#.#...#...#.....#.....#.#.#.#.#.......#.......###.#...#...#...#...#...............###.#.......# +#.#######.#.###.###.###.#.#####.#.#####.#.###.#.#.#.#.#.#####.#######.#.#.#.#.#################.###.#.#.#######.###################.#.####### +#.......#.#...#.....#...#.....#.#.....#.#...#...#.#.#.#.#...#.......#.#.#.#.#.....#.....#.....#...#.#.#.......#...................#...###...# +#######.#.###.#######.#######.#.#####.#.###.#####.#.#.#.#.#.#######.#.#.#.#.#####.#.###.#.###.###.#.#.#######.###################.#######.#.# +#.......#.###.......#.#...#...#.......#.....#...#.#.#.#.#.#...#.....#...#.#.#.....#...#.#...#.###.#.#.......#.#...............#...###...#.#.# +#.#######.#########.#.#.#.#.#################.#.#.#.#.#.#.###.#.#########.#.#.#######.#.###.#.###.#.#######.#.#.#############.#.#####.#.#.#.# +#.......#.#...#.....#...#...#.............###.#...#...#.#...#.#.........#.#.#.....#...#.#...#.....#.....#...#...#.......#...#...#...#.#...#.# +#######.#.#.#.#.#############.###########.###.#########.###.#.#########.#.#.#####.#.###.#.#############.#.#######.#####.#.#.#####.#.#.#####.# +#...#...#...#...#.............#...........#...#.......#.#...#.#...#...#.#.#.#.....#...#...#.....#.....#...###.....#.....#.#...#...#.#.#.....# +#.#.#.###########.#############.###########.###.#####.#.#.###.#.#.#.#.#.#.#.#.#######.#####.###.#.###.#######.#####.#####.###.#.###.#.#.##### +#.#...#.......#...###...........#...#...###...#.....#.#.#...#.#.#.#.#...#...#...#...#...#...###...#...#.......#.....#.....#...#.#...#.#.#...# +#.#####.#####.#.#####.###########.#.#.#.#####.#####.#.#.###.#.#.#.#.###########.#.#.###.#.#########.###.#######.#####.#####.###.#.###.#.#.#.# +#...#...#.....#.#...#...........#.#.#.#.....#.#.....#...###.#.#.#.#...#...#...#.#.#...#...#.......#.....#...#...#...#.#.....#...#.....#...#.# +###.#.###.#####.#.#.###########.#.#.#.#####.#.#.###########.#.#.#.###.#.#.#.#.#.#.###.#####.#####.#######.#.#.###.#.#.#.#####.#############.# +#...#.#...#...#.#.#.#.........#...#...#.....#.#...#...###...#.#.#...#...#.#.#...#.#...#...#.....#.........#...###.#...#.......#...#...#...#.# +#.###.#.###.#.#.#.#.#.#######.#########.#####.###.#.#.###.###.#.###.#####.#.#####.#.###.#.#####.#################.#############.#.#.#.#.#.#.# +#...#.#...#.#.#.#.#...#.....#...#.......#...#.#...#.#.#...#...#.#...#.....#...#...#...#.#.#...#...#.............#...#...........#.#.#.#.#.#.# +###.#.###.#.#.#.#.#####.###.###.#.#######.#.#.#.###.#.#.###.###.#.###.#######.#.#####.#.#.#.#.###.#.###########.###.#.###########.#.#.#.#.#.# +###...#...#.#.#.#.#...#...#...#.#.........#.#.#...#.#.#...#...#.#...#.......#.#.....#.#.#.#.#.###...###.........#...#...........#...#.#.#...# +#######.###.#.#.#.#.#.###.###.#.###########.#.###.#.#.###.###.#.###.#######.#.#####.#.#.#.#.#.#########.#########.#############.#####.#.##### +###...#.....#...#...#.....#...#.............#.#...#.#.###.#...#...#.###...#.#.#...#.#.#.#.#.#.#...#...#...........#.......#...#...###.#.....# +###.#.#####################.#################.#.###.#.###.#.#####.#.###.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#############.#####.#.#.###.###.#####.# +#...#.......###.......#...#.........#.......#.#.#...#...#.#...#...#...#.#.#.#.#.#.#.#.#.#.#.#.#.#...#.#.....#.......#...#...#...#...#.......# +#.#########.###.#####.#.#.#########.#.#####.#.#.#.#####.#.###.#.#####.#.#.#.#.#.#.#.#.#.#.#.#.#.#####.#.###.#.#######.#.#######.###.######### +#.........#.....#...#...#.........#.#...###.#.#...#...#.#...#.#.#.....#.#...#.#.#...#.#.#...#.#.###...#.#...#.........#.......#...#.#.......# +#########.#######.#.#############.#.###.###.#.#####.#.#.###.#.#.#.#####.#####.#.#####.#.#####.#.###.###.#.###################.###.#.#.#####.# +###.....#.......#.#.............#...#...#...#.......#.#...#.#...#...#...#.....#.#.....#.#.....#...#...#.#...#...#...........#.#...#...#.....# +###.###.#######.#.#############.#####.###.###########.###.#.#######.#.###.#####.#.#####.#.#######.###.#.###.#.#.#.#########.#.#.#######.##### +#...#...#.....#...###...........#.....#...#.....#.....###.#.....###.#.###...#...#.#...#.#...#.....#...#...#.#.#.#.....#...#...#.......#.....# +#.###.###.###.#######.###########.#####.###.###.#.#######.#####.###.#.#####.#.###.#.#.#.###.#.#####.#####.#.#.#.#####.#.#.###########.#####.# +#...#...#...#.#.......#.....#...#.#.....#...###.#.......#...#...#...#.....#.#...#.#.#...###.#...###...#...#...#.#...#...#...........#...#...# +###.###.###.#.#.#######.###.#.#.#.#.#####.#####.#######.###.#.###.#######.#.###.#.#.#######.###.#####.#.#######.#.#.###############.###.#.### +#...#...#...#...#...#...#...#.#.#.#.....#.....#...#.....###...###.#.....#.#...#.#.#.......#...#...#...#.#...###...#...#.....#.......#...#...# +#.###.###.#######.#.#.###.###.#.#.#####.#####.###.#.#############.#.###.#.###.#.#.#######.###.###.#.###.#.#.#########.#.###.#.#######.#####.# +#...#.....#...#...#.#.###...#.#...#...#.#...#.#...#.......#.......#...#.#.#...#.#.#.....#...#.#...#...#...#.....#.....#...#.#.......#...#...# +###.#######.#.#.###.#.#####.#.#####.#.#.#.#.#.#.#########.#.#########.#.#.#.###.#.#.###.###.#.#.#####.#########.#.#######.#.#######.###.#.### +###...#...#.#.#.###...#.....#...#...#...#.#...#...#...#...#...#...#...#.#.#...#.#...###...#.#...#...#...#.......#.#.......#.#.......#...#...# +#####.#.#.#.#.#.#######.#######.#.#######.#######.#.#.#.#####.#.#.#.###.#.###.#.#########.#.#####.#.###.#.#######.#.#######.#.#######.#####.# +#.....#.#.#.#.#.#.....#.....#...#...#...#.....###...#.#.....#.#.#.#.###...#...#.....#.....#...#...#.....#.......#.#.....#...#.....###.......# +#.#####.#.#.#.#.#.###.#####.#.#####.#.#.#####.#######.#####.#.#.#.#.#######.#######.#.#######.#.###############.#.#####.#.#######.########### +#...#...#.#.#...#...#.#.....#.#####...#.#...#.....###.#.....#...#.#...#.....#.......#.......#.#.#...#...#.......#...#...#.........#.........# +###.#.###.#.#######.#.#.#####.#########.#.#.#####.###.#.#########.###.#.#####.#############.#.#.#.#.#.#.#.#########.#.#############.#######.# +###.#...#.#.#.......#...#...#...#.......#.#.#.....#...#...#.......#...#.....#.....#.........#.#.#.#.#.#.#.......#...#.........###...#...#...# +###.###.#.#.#.###########.#.###.#.#######.#.#.#####.#####.#.#######.#######.#####.#.#########.#.#.#.#.#.#######.#.###########.###.###.#.#.### +#...#...#...#.....#...#...#...#.#...#...#.#.#.....#.#...#.#.........#...###.#...#.#.#.......#.#...#.#.#.#.......#.....#.....#.....#...#.#...# +#.###.###########.#.#.#.#####.#.###.#.#.#.#.#####.#.#.#.#.###########.#.###.#.#.#.#.#.#####.#.#####.#.#.#.###########.#.###.#######.###.###.# +#...#.....#####...#.#.#.....#.#.#...#.#.#.#...#...#...#.#.....#.......#.....#.#.#.#...#.....#...#...#.#.#...........#.#...#.#...#...#...#...# +###.#####.#####.###.#.#####.#.#.#.###.#.#.###.#.#######.#####.#.#############.#.#.#####.#######.#.###.#.###########.#.###.#.#.#.#.###.###.### +#...#...#...#...#...#.....#.#...#...#.#.#.###.#.......#...#...#.....#...#...#.#.#.....#.#...#...#...#.#.#.....#...#.#.#...#...#...###.#...### +#.###.#.###.#.###.#######.#.#######.#.#.#.###.#######.###.#.#######.#.#.#.#.#.#.#####.#.#.#.#.#####.#.#.#.###.#.#.#.#.#.#############.#.##### +#.....#.....#...#.......#.#.#.......#.#...#...#...#...###.#...#.....#.#...#.#.#.#.....#...#.#.#...#...#.#...#.#.#.#.#.#.............#...#...# +###############.#######.#.#.#.#######.#####.###.#.#.#####.###.#.#####.#####.#.#.#.#########.#.#.#.#####.###.#.#.#.#.#.#############.#####.#.# +###...#.........#...#...#...#.....#...###...#...#...###...#...#.#...#...#...#.#.#...#.......#...#.###...#...#.#.#.#.#.#...#...#...#.....#.#.# +###.#.#.#########.#.#.###########.#.#####.###.#########.###.###.#.#.###.#.###.#.###.#.###########.###.###.###.#.#.#.#.#.#.#.#.#.#.#####.#.#.# +#...#...#.....#...#.#...#.........#.....#...#.........#...#...#.#.#...#.#...#.#...#.#.#.....#...#.#...#...#...#.#.#.#.#.#.#.#.#.#.#...#...#.# +#.#######.###.#.###.###.#.#############.###.#########.###.###.#.#.###.#.###.#.###.#.#.#.###.#.#.#.#.###.###.###.#.#.#.#.#.#.#.#.#.#.#.#####.# +#.........###...###.....#...............###...........###.....#...###...###...###...#...###...#...#.....###.....#...#...#...#...#...#.......# +############################################################################################################################################# diff --git a/src/holt59/aoc/inputs/tests/2024/day20.txt b/src/holt59/aoc/inputs/tests/2024/day20.txt index e69de29..c097dae 100644 --- a/src/holt59/aoc/inputs/tests/2024/day20.txt +++ b/src/holt59/aoc/inputs/tests/2024/day20.txt @@ -0,0 +1,15 @@ +############### +#...#...#.....# +#.#.#.#.#.###.# +#S#...#.#.#...# +#######.#.#.### +#######.#.#...# +#######.#.###.# +###..E#...#...# +###.#######.### +#...###...#...# +#.#####.#.###.# +#.#...#.#.#...# +#.#.#.#.#.#.### +#...#...#...### +############### diff --git a/src/holt59/aoc/tools/graphs.py b/src/holt59/aoc/tools/graphs.py index 4f301ae..e475025 100644 --- a/src/holt59/aoc/tools/graphs.py +++ b/src/holt59/aoc/tools/graphs.py @@ -1,5 +1,5 @@ import heapq -from typing import Callable, Iterable, TypeVar +from typing import Callable, Iterable, TypeVar, overload _Node = TypeVar("_Node") @@ -53,11 +53,31 @@ def make_neighbors_grid_fn( return _fn +@overload +def dijkstra( + start: _Node, + target: None, + neighbors: Callable[[_Node], Iterable[tuple[_Node, float]]], +) -> dict[_Node, tuple[tuple[_Node, ...], float]]: ... + + +@overload def dijkstra( start: _Node, target: _Node, neighbors: Callable[[_Node], Iterable[tuple[_Node, float]]], -) -> tuple[tuple[_Node, ...], float] | None: +) -> tuple[tuple[_Node, ...], float] | None: ... + + +def dijkstra( + start: _Node, + target: _Node | None, + neighbors: Callable[[_Node], Iterable[tuple[_Node, float]]], +) -> ( + dict[_Node, tuple[tuple[_Node, ...], float]] + | tuple[tuple[_Node, ...], float] + | None +): """ Solve shortest-path problem using simple Dijkstra algorithm from start to target, using the given neighbors function. @@ -92,4 +112,7 @@ def dijkstra( heapq.heappush(queue, (dis + cost, neighbor, path + (neighbor,))) + if target is None: + return preds + return preds.get(target, None)