Fix linting.
This commit is contained in:
@@ -2,24 +2,34 @@ import logging
|
||||
import os
|
||||
import sys
|
||||
from collections import defaultdict
|
||||
from typing import Literal, Sequence, TypeAlias, cast
|
||||
|
||||
VERBOSE = os.getenv("AOC_VERBOSE") == "True"
|
||||
logging.basicConfig(level=logging.INFO if VERBOSE else logging.WARNING)
|
||||
|
||||
Direction = {
|
||||
DirectionType: TypeAlias = Literal[">", "<", "^", "v", ".", "#"]
|
||||
|
||||
Direction: dict[DirectionType, tuple[int, int]] = {
|
||||
">": (0, +1),
|
||||
"<": (0, -1),
|
||||
"^": (-1, 0),
|
||||
"v": (+1, 0),
|
||||
}
|
||||
|
||||
Neighbors = {".": ((+1, 0), (-1, 0), (0, +1), (0, -1)), "#": ()} | {
|
||||
k: (v,) for k, v in Direction.items()
|
||||
}
|
||||
Neighbors = cast(
|
||||
"dict[DirectionType, tuple[tuple[int, int], ...]]",
|
||||
{
|
||||
".": ((+1, 0), (-1, 0), (0, +1), (0, -1)),
|
||||
"#": (),
|
||||
}
|
||||
| {k: (v,) for k, v in Direction.items()},
|
||||
)
|
||||
|
||||
|
||||
def neighbors(
|
||||
grid: list[str], node: tuple[int, int], ignore: set[tuple[int, int]] = set()
|
||||
grid: list[Sequence[DirectionType]],
|
||||
node: tuple[int, int],
|
||||
ignore: set[tuple[int, int]] = set(),
|
||||
):
|
||||
"""
|
||||
Compute neighbors of the given node, ignoring the given set of nodes and considering
|
||||
@@ -51,7 +61,7 @@ def neighbors(
|
||||
|
||||
|
||||
def reachable(
|
||||
grid: list[str], start: tuple[int, int], target: tuple[int, int]
|
||||
grid: list[Sequence[DirectionType]], start: tuple[int, int], target: tuple[int, int]
|
||||
) -> tuple[tuple[int, int], int]:
|
||||
"""
|
||||
Compute the next 'reachable' node in the grid, starting at the given node.
|
||||
@@ -75,7 +85,7 @@ def reachable(
|
||||
|
||||
|
||||
def compute_direct_links(
|
||||
grid: list[str], start: tuple[int, int], target: tuple[int, int]
|
||||
grid: list[Sequence[DirectionType]], start: tuple[int, int], target: tuple[int, int]
|
||||
) -> dict[tuple[int, int], list[tuple[tuple[int, int], int]]]:
|
||||
if start == target:
|
||||
return {}
|
||||
@@ -85,7 +95,7 @@ def compute_direct_links(
|
||||
i, j = neighbor
|
||||
di, dj = Direction[grid[i][j]]
|
||||
|
||||
reach, distance = reachable(lines, (i + di, j + dj), target)
|
||||
reach, distance = reachable(grid, (i + di, j + dj), target)
|
||||
direct[start].append((reach, distance + 2))
|
||||
|
||||
direct.update(compute_direct_links(grid, reach, target))
|
||||
@@ -124,7 +134,7 @@ def longest_path_length(
|
||||
return max_distance
|
||||
|
||||
|
||||
lines = sys.stdin.read().splitlines()
|
||||
lines = cast(list[Sequence[DirectionType]], sys.stdin.read().splitlines())
|
||||
n_rows, n_cols = len(lines), len(lines[0])
|
||||
start = (0, 1)
|
||||
target = (len(lines) - 1, len(lines[0]) - 2)
|
||||
|
Reference in New Issue
Block a user