2024 day 20.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Mikaël Capelle 2024-12-20 09:19:12 +01:00
parent 683cac334c
commit 96f139fe10
4 changed files with 271 additions and 4 deletions

View File

@ -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 ..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): 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())

View File

@ -0,0 +1,141 @@
#############################################################################################################################################
#.......#.........#.....#.....#...#...###...###.....###...#...###.....#.....#.......#.......#.....#...#####.....#...#...#...#...............#
#.#####.#.#######.#.###.#.###.#.#.#.#.###.#.###.###.###.#.#.#.###.###.#.###.#.#####.#.#####.#.###.#.#.#####.###.#.#.#.#.#.#.#.#############.#
#...#...#.......#.#...#.#...#.#.#...#...#.#...#...#.....#.#.#...#...#.#...#.#.....#.#...#...#.#...#.#...#...#...#.#...#.#.#.#...#...........#
###.#.#########.#.###.#.###.#.#.#######.#.###.###.#######.#.###.###.#.###.#.#####.#.###.#.###.#.###.###.#.###.###.#####.#.#.###.#.###########
#...#.....#.....#.#...#.#...#.#.......#.#.#...###.#.......#.#...#...#.....#.......#...#.#...#.#...#.#...#...#.###.....#...#...#.#...........#
#.#######.#.#####.#.###.#.###.#######.#.#.#.#####.#.#######.#.###.###################.#.###.#.###.#.#.#####.#.#######.#######.#.###########.#
#...#.....#.....#.#.#...#...#...#...#.#...#...#...#.......#.#...#...........#.......#...#...#.#...#.#...#...#...#...#.#.......#.#.........#.#
###.#.#########.#.#.#.#####.###.#.#.#.#######.#.#########.#.###.###########.#.#####.#####.###.#.###.###.#.#####.#.#.#.#.#######.#.#######.#.#
###.#.###.....#.#.#.#.#...#.#...#.#.#...#.....#.....#.....#.#...#...###...#...#...#.#.....#...#...#.#...#...#...#.#.#.#.###...#.#.#.....#.#.#
###.#.###.###.#.#.#.#.#.#.#.#.###.#.###.#.#########.#.#####.#.###.#.###.#.#####.#.#.#.#####.#####.#.#.#####.#.###.#.#.#.###.#.#.#.#.###.#.#.#
#...#...#...#.#.#.#.#.#.#...#.#...#.....#...###.....#...#...#...#.#.#...#.###...#...#...###.....#.#.#.#.....#...#.#.#.#.....#...#...#...#...#
#.#####.###.#.#.#.#.#.#.#####.#.###########.###.#######.#.#####.#.#.#.###.###.#########.#######.#.#.#.#.#######.#.#.#.###############.#######
#.....#.....#...#.#.#.#.....#.#...........#...#.#.....#.#.....#.#.#.#.#...#...#.......#.#...###.#.#.#.#.....#...#.#...#.............#.......#
#####.###########.#.#.#####.#.###########.###.#.#.###.#.#####.#.#.#.#.#.###.###.#####.#.#.#.###.#.#.#.#####.#.###.#####.###########.#######.#
#...#.........#...#.#.#...#.#...#...#...#.#...#.#...#...#.....#.#.#...#.###...#.#.....#...#...#.#.#.#.....#.#.###.#...#...........#...#.....#
#.#.#########.#.###.#.#.#.#.###.#.#.#.#.#.#.###.###.#####.#####.#.#####.#####.#.#.###########.#.#.#.#####.#.#.###.#.#.###########.###.#.#####
#.#...........#...#.#.#.#.#...#...#.#.#.#.#.....#...#...#...#...#...#...#...#...#...#.....#...#.#...#.....#.#...#.#.#.......#...#...#.#.....#
#.###############.#.#.#.#.###.#####.#.#.#.#######.###.#.###.#.#####.#.###.#.#######.#.###.#.###.#####.#####.###.#.#.#######.#.#.###.#.#####.#
#...#.....#.....#...#...#...#.....#.#.#.#.#.......#...#.....#...#...#...#.#...#...#.#...#...###.....#.#...#...#.#.#.#...###.#.#.#...#.......#
###.#.###.#.###.###########.#####.#.#.#.#.#.#######.###########.#.#####.#.###.#.#.#.###.###########.#.#.#.###.#.#.#.#.#.###.#.#.#.###########
###...###...#...#...#...#...#...#.#.#.#.#.#...#...#.........#...#.....#.#.#...#.#...#...#...#...#...#...#.....#...#...#...#.#.#...#...#...###
#############.###.#.#.#.#.###.#.#.#.#.#.#.###.#.#.#########.#.#######.#.#.#.###.#####.###.#.#.#.#.#######################.#.#.#####.#.#.#.###
#.............#...#...#.#...#.#.#.#.#.#.#.#...#.#.#...###...#.#...#...#...#...#...#...#...#.#.#.#.....#.........#.........#.#.....#.#.#.#...#
#.#############.#######.###.#.#.#.#.#.#.#.#.###.#.#.#.###.###.#.#.#.#########.###.#.###.###.#.#.#####.#.#######.#.#########.#####.#.#.#.###.#
#.#.............###...#...#.#.#...#...#...#.....#.#.#.#...#...#.#.#...#.......#...#...#.#...#.#.#...#.#.......#.#.......###.......#.#.#.#...#
#.#.###############.#.###.#.#.###################.#.#.#.###.###.#.###.#.#######.#####.#.#.###.#.#.#.#.#######.#.#######.###########.#.#.#.###
#...#...#...........#.....#...#...#...#...#.......#.#.#...#...#.#.#...#.#.....#.....#.#.#...#.#.#.#...#.......#...#...#.......#.....#...#...#
#####.#.#.#####################.#.#.#.#.#.#.#######.#.###.###.#.#.#.###.#.###.#####.#.#.###.#.#.#.#####.#########.#.#.#######.#.###########.#
#...#.#.#.........#.......#.....#...#...#.#.#...###.#.#...#...#.#.#...#...#...#.....#.#...#.#.#.#.#...#...###...#...#.........#...#.....#...#
#.#.#.#.#########.#.#####.#.#############.#.#.#.###.#.#.###.###.#.###.#####.###.#####.###.#.#.#.#.#.#.###.###.#.#################.#.###.#.###
#.#...#.......#...#.#.....#...#...#...#...#...#.#...#.#...#...#.#...#.#...#...#.....#...#.#...#.#...#.#...#...#.......###...#...#.#...#.#...#
#.###########.#.###.#.#######.#.#.#.#.#.#######.#.###.###.###.#.###.#.#.#.###.#####.###.#.#####.#####.#.###.#########.###.#.#.#.#.###.#.###.#
#.........#...#.....#.........#.#...#.#.#.......#...#.###.#...#.#...#...#.#...#...#.#...#.#.....#...#.#.###.#.......#.#...#...#.#.#...#.....#
#########.#.###################.#####.#.#.#########.#.###.#.###.#.#######.#.###.#.#.#.###.#.#####.#.#.#.###.#.#####.#.#.#######.#.#.#########
###...###.#.#...#...#.........#.....#...#...#.....#.#.#...#.#...#...#...#.#.....#.#.#.#...#...#...#...#.....#.....#.#.#...#.....#.#.....#...#
###.#.###.#.#.#.#.#.#.#######.#####.#######.#.###.#.#.#.###.#.#####.#.#.#.#######.#.#.#.#####.#.#################.#.#.###.#.#####.#####.#.#.#
#...#.....#...#.#.#.#.....#...#...#.......#.#.#...#.#.#...#.#.....#.#.#.#.....###.#.#.#.#.....#.....###.........#.#...###.#.......#...#...#.#
#.#############.#.#.#####.#.###.#.#######.#.#.#.###.#.###.#.#####.#.#.#.#####.###.#.#.#.#.#########.###.#######.#.#######.#########.#.#####.#
#.#...........#...#.......#.#...#.#.......#.#.#...#.#.#...#.#.....#.#.#.#...#.#...#.#.#.#...#.....#...#.......#...#.......#.........#.....#.#
#.#.#########.#############.#.###.#.#######.#.###.#.#.#.###.#.#####.#.#.#.#.#.#.###.#.#.###.#.###.###.#######.#####.#######.#############.#.#
#...#.....###.............#.#...#...#.....#...###.#.#.#.#...#...#...#.#.#.#.#.#...#.#...#...#.###.#...#.....#...#...#...#...#...#...#...#...#
#####.###.###############.#.###.#####.###.#######.#.#.#.#.#####.#.###.#.#.#.#.###.#.#####.###.###.#.###.###.###.#.###.#.#.###.#.#.#.#.#.#####
#...#...#.....#...........#.....#...#.#...#.....#...#...#...#...#...#.#.#.#.#S###.#.....#...#...#...###...#...#...#...#.#.....#.#.#...#...###
#.#.###.#####.#.#################.#.#.#.###.###.###########.#.#####.#.#.#.#.#####.#####.###.###.#########.###.#####.###.#######.#.#######.###
#.#.###.....#...#...#.....#.......#...#...#.#...#...#.......#.#...#...#...#...###.#.....#...#...#...#...#...#.#...#...#.........#.......#...#
#.#.#######.#####.#.#.###.#.#############.#.#.###.#.#.#######.#.#.###########.###.#.#####.###.###.#.#.#.###.#.#.#.###.#################.###.#
#.#.#...#...#...#.#.#.###.#.###.........#...#.....#.#.#.....#...#.......#.....###.#...#...#...###.#...#.....#...#.....#.....#...#.......#...#
#.#.#.#.#.###.#.#.#.#.###.#.###.#######.###########.#.#.###.###########.#.#######.###.#.###.#####.#####################.###.#.#.#.#######.###
#.#.#.#.#...#.#...#...#...#.#...#.....#.#...#...#...#.#.#...#.....#...#.#.#...###.#...#...#.....#...#...........###...#...#.#.#.#.#.......###
#.#.#.#.###.#.#########.###.#.###.###.#.#.#.#.#.#.###.#.#.###.###.#.#.#.#.#.#.###.#.#####.#####.###.#.#########.###.#.###.#.#.#.#.#.#########
#.#...#...#...###...###...#.#.....#...#.#.#...#...###...#...#...#.#.#...#...#.###.#.#.....#...#.#...#.#.........#...#...#.#...#...#...#...###
#.#######.#######.#.#####.#.#######.###.#.#################.###.#.#.#########.###.#.#.#####.#.#.#.###.#.#########.#####.#.###########.#.#.###
#.......#.....#...#.#.....#.#.......###...#.........#.......#...#...###...#...###...#...#...#...#.....#...........#...#.#.#...........#.#...#
#######.#####.#.###.#.#####.#.#############.#######.#.#######.#########.#.#.###########.#.#########################.#.#.#.#.###########.###.#
#...###.#.....#...#...#...#.#...#...#.....#.#.......#.....#...###...#...#.#...#######...#.###.................#...#.#...#.#...#...#.....#...#
#.#.###.#.#######.#####.#.#.###.#.#.#.###.#.#.###########.#.#####.#.#.###.###.#######.###.###.###############.#.#.#.#####.###.#.#.#.#####.###
#.#.....#.....###...#...#...###...#...#...#.#.....###...#.#...#...#.#...#.....#######...#.#...#.......#.....#.#.#...#...#.#...#.#...#...#...#
#.###########.#####.#.#################.###.#####.###.#.#.###.#.###.###.###############.#.#.###.#####.#.###.#.#.#####.#.#.#.###.#####.#.###.#
#...#.......#.#...#...###...#...#.......###.#.....#...#.#.....#...#.#...#.....#######...#.#...#.#.....#.#...#...#.....#...#.....#.....#.....#
###.#.#####.#.#.#.#######.#.#.#.#.#########.#.#####.###.#########.#.#.###.###.#######.###.###.#.#.#####.#.#######.###############.###########
#...#.#...#.#.#.#.........#.#.#.#.......#...#.....#...#.#...#...#.#...#...#...#######...#.###...#.......#.........#.......#.....#...........#
#.###.#.#.#.#.#.###########.#.#.#######.#.#######.###.#.#.#.#.#.#.#####.###.###########.#.#########################.#####.#.###.###########.#
#.#...#.#...#...#.........#...#.........#.#.....#...#.#.#.#.#.#.#.#...#...#...#...###E#...#...###...........#.....#.....#.#...#.#.....#.....#
#.#.###.#########.#######.###############.#.###.###.#.#.#.#.#.#.#.#.#.###.###.#.#.###.#####.#.###.#########.#.###.#####.#.###.#.#.###.#.#####
#...###.....#...#.#...###.................#.###.....#.#...#.#.#.#...#.#...#...#.#.#...#.....#.#...#...#...#...###.......#.#...#.#...#.#.....#
###########.#.#.#.#.#.#####################.#########.#####.#.#.#####.#.###.###.#.#.###.#####.#.###.#.#.#.###############.#.###.###.#.#####.#
#...###...#...#...#.#.#...#...#...#...#...#...#.......#...#...#.....#...###...#.#...###.....#.#...#.#...#.......#.......#.#...#...#.#...#...#
#.#.###.#.#########.#.#.#.#.#.#.#.#.#.#.#.###.#.#######.#.#########.#########.#.###########.#.###.#.###########.#.#####.#.###.###.#.###.#.###
#.#.#...#...#...#...#...#...#...#...#...#...#...#.....#.#.........#.#.....###...#.....#...#.#.###...#...........#.#.....#.....#...#...#...###
#.#.#.#####.#.#.#.#########################.#####.###.#.#########.#.#.###.#######.###.#.#.#.#.#######.###########.#.###########.#####.#######
#.#.#.....#.#.#.#.......................###.#.....###.#.........#...#...#.......#...#.#.#...#.#...###.............#.....###...#...#...#...###
#.#.#####.#.#.#.#######################.###.#.#######.#########.#######.#######.###.#.#.#####.#.#.#####################.###.#.###.#.###.#.###
#.#.#...#.#...#.......................#...#...#.....#...........#...#...#.......#...#.#.....#.#.#.#...###.....#.......#.....#...#...#...#...#
#.#.#.#.#.###########################.###.#####.###.#############.#.#.###.#######.###.#####.#.#.#.#.#.###.###.#.#####.#########.#####.#####.#
#.#...#...#...#.......#.....#...#...#.....#...#.#...###...#...###.#.#...#.....###...#.###...#...#...#.....###.#.....#...#...#...#...#...#...#
#.#########.#.#.#####.#.###.#.#.#.#.#######.#.#.#.#####.#.#.#.###.#.###.#####.#####.#.###.###################.#####.###.#.#.#.###.#.###.#.###
#...#.....#.#.#.#.....#.#...#.#...#.....###.#.#.#.......#.#.#.#...#...#.#.....#...#.#.....#.....#...#.......#.#...#.###...#...#...#.....#...#
###.#.###.#.#.#.#.#####.#.###.#########.###.#.#.#########.#.#.#.#####.#.#.#####.#.#.#######.###.#.#.#.#####.#.#.#.#.###########.###########.#
#...#...#.#.#.#.#...#...#...#.#.........#...#...#...#.....#.#.#.....#.#.#.#...#.#.#.......#...#...#.#.#.....#...#.#.#...#...#...#...#.......#
#.#####.#.#.#.#.###.#.#####.#.#.#########.#######.#.#.#####.#.#####.#.#.#.#.#.#.#.#######.###.#####.#.#.#########.#.#.#.#.#.#.###.#.#.#######
#.......#...#...###...#####...#...........#...#...#...###...#...###.#.#.#.#.#...#...#.....#...#...#...#...#...###.#...#...#...#...#.#.......#
###########################################.#.#.#########.#####.###.#.#.#.#.#######.#.#####.###.#.#######.#.#.###.#############.###.#######.#
#.........#.....###.....#.......#.......#...#.#.#...#...#.....#.....#.#.#.#.#.......#.......###.#...#...#...#...#...............###.#.......#
#.#######.#.###.###.###.#.#####.#.#####.#.###.#.#.#.#.#.#####.#######.#.#.#.#.#################.###.#.#.#######.###################.#.#######
#.......#.#...#.....#...#.....#.#.....#.#...#...#.#.#.#.#...#.......#.#.#.#.#.....#.....#.....#...#.#.#.......#...................#...###...#
#######.#.###.#######.#######.#.#####.#.###.#####.#.#.#.#.#.#######.#.#.#.#.#####.#.###.#.###.###.#.#.#######.###################.#######.#.#
#.......#.###.......#.#...#...#.......#.....#...#.#.#.#.#.#...#.....#...#.#.#.....#...#.#...#.###.#.#.......#.#...............#...###...#.#.#
#.#######.#########.#.#.#.#.#################.#.#.#.#.#.#.###.#.#########.#.#.#######.#.###.#.###.#.#######.#.#.#############.#.#####.#.#.#.#
#.......#.#...#.....#...#...#.............###.#...#...#.#...#.#.........#.#.#.....#...#.#...#.....#.....#...#...#.......#...#...#...#.#...#.#
#######.#.#.#.#.#############.###########.###.#########.###.#.#########.#.#.#####.#.###.#.#############.#.#######.#####.#.#.#####.#.#.#####.#
#...#...#...#...#.............#...........#...#.......#.#...#.#...#...#.#.#.#.....#...#...#.....#.....#...###.....#.....#.#...#...#.#.#.....#
#.#.#.###########.#############.###########.###.#####.#.#.###.#.#.#.#.#.#.#.#.#######.#####.###.#.###.#######.#####.#####.###.#.###.#.#.#####
#.#...#.......#...###...........#...#...###...#.....#.#.#...#.#.#.#.#...#...#...#...#...#...###...#...#.......#.....#.....#...#.#...#.#.#...#
#.#####.#####.#.#####.###########.#.#.#.#####.#####.#.#.###.#.#.#.#.###########.#.#.###.#.#########.###.#######.#####.#####.###.#.###.#.#.#.#
#...#...#.....#.#...#...........#.#.#.#.....#.#.....#...###.#.#.#.#...#...#...#.#.#...#...#.......#.....#...#...#...#.#.....#...#.....#...#.#
###.#.###.#####.#.#.###########.#.#.#.#####.#.#.###########.#.#.#.###.#.#.#.#.#.#.###.#####.#####.#######.#.#.###.#.#.#.#####.#############.#
#...#.#...#...#.#.#.#.........#...#...#.....#.#...#...###...#.#.#...#...#.#.#...#.#...#...#.....#.........#...###.#...#.......#...#...#...#.#
#.###.#.###.#.#.#.#.#.#######.#########.#####.###.#.#.###.###.#.###.#####.#.#####.#.###.#.#####.#################.#############.#.#.#.#.#.#.#
#...#.#...#.#.#.#.#...#.....#...#.......#...#.#...#.#.#...#...#.#...#.....#...#...#...#.#.#...#...#.............#...#...........#.#.#.#.#.#.#
###.#.###.#.#.#.#.#####.###.###.#.#######.#.#.#.###.#.#.###.###.#.###.#######.#.#####.#.#.#.#.###.#.###########.###.#.###########.#.#.#.#.#.#
###...#...#.#.#.#.#...#...#...#.#.........#.#.#...#.#.#...#...#.#...#.......#.#.....#.#.#.#.#.###...###.........#...#...........#...#.#.#...#
#######.###.#.#.#.#.#.###.###.#.###########.#.###.#.#.###.###.#.###.#######.#.#####.#.#.#.#.#.#########.#########.#############.#####.#.#####
###...#.....#...#...#.....#...#.............#.#...#.#.###.#...#...#.###...#.#.#...#.#.#.#.#.#.#...#...#...........#.......#...#...###.#.....#
###.#.#####################.#################.#.###.#.###.#.#####.#.###.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#############.#####.#.#.###.###.#####.#
#...#.......###.......#...#.........#.......#.#.#...#...#.#...#...#...#.#.#.#.#.#.#.#.#.#.#.#.#.#...#.#.....#.......#...#...#...#...#.......#
#.#########.###.#####.#.#.#########.#.#####.#.#.#.#####.#.###.#.#####.#.#.#.#.#.#.#.#.#.#.#.#.#.#####.#.###.#.#######.#.#######.###.#########
#.........#.....#...#...#.........#.#...###.#.#...#...#.#...#.#.#.....#.#...#.#.#...#.#.#...#.#.###...#.#...#.........#.......#...#.#.......#
#########.#######.#.#############.#.###.###.#.#####.#.#.###.#.#.#.#####.#####.#.#####.#.#####.#.###.###.#.###################.###.#.#.#####.#
###.....#.......#.#.............#...#...#...#.......#.#...#.#...#...#...#.....#.#.....#.#.....#...#...#.#...#...#...........#.#...#...#.....#
###.###.#######.#.#############.#####.###.###########.###.#.#######.#.###.#####.#.#####.#.#######.###.#.###.#.#.#.#########.#.#.#######.#####
#...#...#.....#...###...........#.....#...#.....#.....###.#.....###.#.###...#...#.#...#.#...#.....#...#...#.#.#.#.....#...#...#.......#.....#
#.###.###.###.#######.###########.#####.###.###.#.#######.#####.###.#.#####.#.###.#.#.#.###.#.#####.#####.#.#.#.#####.#.#.###########.#####.#
#...#...#...#.#.......#.....#...#.#.....#...###.#.......#...#...#...#.....#.#...#.#.#...###.#...###...#...#...#.#...#...#...........#...#...#
###.###.###.#.#.#######.###.#.#.#.#.#####.#####.#######.###.#.###.#######.#.###.#.#.#######.###.#####.#.#######.#.#.###############.###.#.###
#...#...#...#...#...#...#...#.#.#.#.....#.....#...#.....###...###.#.....#.#...#.#.#.......#...#...#...#.#...###...#...#.....#.......#...#...#
#.###.###.#######.#.#.###.###.#.#.#####.#####.###.#.#############.#.###.#.###.#.#.#######.###.###.#.###.#.#.#########.#.###.#.#######.#####.#
#...#.....#...#...#.#.###...#.#...#...#.#...#.#...#.......#.......#...#.#.#...#.#.#.....#...#.#...#...#...#.....#.....#...#.#.......#...#...#
###.#######.#.#.###.#.#####.#.#####.#.#.#.#.#.#.#########.#.#########.#.#.#.###.#.#.###.###.#.#.#####.#########.#.#######.#.#######.###.#.###
###...#...#.#.#.###...#.....#...#...#...#.#...#...#...#...#...#...#...#.#.#...#.#...###...#.#...#...#...#.......#.#.......#.#.......#...#...#
#####.#.#.#.#.#.#######.#######.#.#######.#######.#.#.#.#####.#.#.#.###.#.###.#.#########.#.#####.#.###.#.#######.#.#######.#.#######.#####.#
#.....#.#.#.#.#.#.....#.....#...#...#...#.....###...#.#.....#.#.#.#.###...#...#.....#.....#...#...#.....#.......#.#.....#...#.....###.......#
#.#####.#.#.#.#.#.###.#####.#.#####.#.#.#####.#######.#####.#.#.#.#.#######.#######.#.#######.#.###############.#.#####.#.#######.###########
#...#...#.#.#...#...#.#.....#.#####...#.#...#.....###.#.....#...#.#...#.....#.......#.......#.#.#...#...#.......#...#...#.........#.........#
###.#.###.#.#######.#.#.#####.#########.#.#.#####.###.#.#########.###.#.#####.#############.#.#.#.#.#.#.#.#########.#.#############.#######.#
###.#...#.#.#.......#...#...#...#.......#.#.#.....#...#...#.......#...#.....#.....#.........#.#.#.#.#.#.#.......#...#.........###...#...#...#
###.###.#.#.#.###########.#.###.#.#######.#.#.#####.#####.#.#######.#######.#####.#.#########.#.#.#.#.#.#######.#.###########.###.###.#.#.###
#...#...#...#.....#...#...#...#.#...#...#.#.#.....#.#...#.#.........#...###.#...#.#.#.......#.#...#.#.#.#.......#.....#.....#.....#...#.#...#
#.###.###########.#.#.#.#####.#.###.#.#.#.#.#####.#.#.#.#.###########.#.###.#.#.#.#.#.#####.#.#####.#.#.#.###########.#.###.#######.###.###.#
#...#.....#####...#.#.#.....#.#.#...#.#.#.#...#...#...#.#.....#.......#.....#.#.#.#...#.....#...#...#.#.#...........#.#...#.#...#...#...#...#
###.#####.#####.###.#.#####.#.#.#.###.#.#.###.#.#######.#####.#.#############.#.#.#####.#######.#.###.#.###########.#.###.#.#.#.#.###.###.###
#...#...#...#...#...#.....#.#...#...#.#.#.###.#.......#...#...#.....#...#...#.#.#.....#.#...#...#...#.#.#.....#...#.#.#...#...#...###.#...###
#.###.#.###.#.###.#######.#.#######.#.#.#.###.#######.###.#.#######.#.#.#.#.#.#.#####.#.#.#.#.#####.#.#.#.###.#.#.#.#.#.#############.#.#####
#.....#.....#...#.......#.#.#.......#.#...#...#...#...###.#...#.....#.#...#.#.#.#.....#...#.#.#...#...#.#...#.#.#.#.#.#.............#...#...#
###############.#######.#.#.#.#######.#####.###.#.#.#####.###.#.#####.#####.#.#.#.#########.#.#.#.#####.###.#.#.#.#.#.#############.#####.#.#
###...#.........#...#...#...#.....#...###...#...#...###...#...#.#...#...#...#.#.#...#.......#...#.###...#...#.#.#.#.#.#...#...#...#.....#.#.#
###.#.#.#########.#.#.###########.#.#####.###.#########.###.###.#.#.###.#.###.#.###.#.###########.###.###.###.#.#.#.#.#.#.#.#.#.#.#####.#.#.#
#...#...#.....#...#.#...#.........#.....#...#.........#...#...#.#.#...#.#...#.#...#.#.#.....#...#.#...#...#...#.#.#.#.#.#.#.#.#.#.#...#...#.#
#.#######.###.#.###.###.#.#############.###.#########.###.###.#.#.###.#.###.#.###.#.#.#.###.#.#.#.#.###.###.###.#.#.#.#.#.#.#.#.#.#.#.#####.#
#.........###...###.....#...............###...........###.....#...###...###...###...#...###...#...#.....###.....#...#...#...#...#...#.......#
#############################################################################################################################################

View File

@ -0,0 +1,15 @@
###############
#...#...#.....#
#.#.#.#.#.###.#
#S#...#.#.#...#
#######.#.#.###
#######.#.#...#
#######.#.###.#
###..E#...#...#
###.#######.###
#...###...#...#
#.#####.#.###.#
#.#...#.#.#...#
#.#.#.#.#.#.###
#...#...#...###
###############

View File

@ -1,5 +1,5 @@
import heapq import heapq
from typing import Callable, Iterable, TypeVar from typing import Callable, Iterable, TypeVar, overload
_Node = TypeVar("_Node") _Node = TypeVar("_Node")
@ -53,11 +53,31 @@ def make_neighbors_grid_fn(
return _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( def dijkstra(
start: _Node, start: _Node,
target: _Node, target: _Node,
neighbors: Callable[[_Node], Iterable[tuple[_Node, float]]], 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, Solve shortest-path problem using simple Dijkstra algorithm from start to target,
using the given neighbors function. using the given neighbors function.
@ -92,4 +112,7 @@ def dijkstra(
heapq.heappush(queue, (dis + cost, neighbor, path + (neighbor,))) heapq.heappush(queue, (dis + cost, neighbor, path + (neighbor,)))
if target is None:
return preds
return preds.get(target, None) return preds.get(target, None)