|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import heapq |
| 4 | +import itertools |
| 5 | +from dataclasses import dataclass |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from frozendict import frozendict |
| 9 | + |
| 10 | + |
| 11 | +def part_1(input: Path) -> int: |
| 12 | + start, end, walls = parse_input(input) |
| 13 | + lowest_scores = find_lowest_scores(start, end, walls) |
| 14 | + return min([score for state, score in lowest_scores.items() if state.p == end]) |
| 15 | + |
| 16 | + |
| 17 | +def parse_input(input: Path) -> tuple[P, P, frozenset[P]]: |
| 18 | + start: P | None = None |
| 19 | + end: P | None = None |
| 20 | + walls: set[P] = set() |
| 21 | + for y, line in enumerate(input.read_text().splitlines()): |
| 22 | + for x, c in enumerate(line): |
| 23 | + if c == "#": |
| 24 | + walls.add(P(x, y)) |
| 25 | + elif c == "S": |
| 26 | + start = P(x, y) |
| 27 | + elif c == "E": |
| 28 | + end = P(x, y) |
| 29 | + assert start is not None |
| 30 | + assert end is not None |
| 31 | + return start, end, frozenset(walls) |
| 32 | + |
| 33 | + |
| 34 | +def find_lowest_scores(start: P, end: P, walls: frozenset[P]) -> frozendict[State, int]: |
| 35 | + counter = itertools.count() |
| 36 | + q = [(0, next(counter), State(start, P(1, 0)))] |
| 37 | + scores = {} |
| 38 | + while q: |
| 39 | + score, _, state = heapq.heappop(q) |
| 40 | + if state in scores: |
| 41 | + continue |
| 42 | + |
| 43 | + scores[state] = score |
| 44 | + |
| 45 | + heapq.heappush( |
| 46 | + q, |
| 47 | + (score + 1000, next(counter), State(state.p, CLOCKWISE[state.d])), # ty: ignore[invalid-argument-type] |
| 48 | + ) |
| 49 | + heapq.heappush( |
| 50 | + q, |
| 51 | + (score + 1000, next(counter), State(state.p, ANTICLOCKWISE[state.d])), # ty: ignore[invalid-argument-type] |
| 52 | + ) |
| 53 | + if state.p + state.d not in walls: |
| 54 | + heapq.heappush( |
| 55 | + q, |
| 56 | + (score + 1, next(counter), State(state.p + state.d, state.d)), |
| 57 | + ) |
| 58 | + |
| 59 | + return frozendict(scores) |
| 60 | + |
| 61 | + |
| 62 | +@dataclass(frozen=True) |
| 63 | +class State: |
| 64 | + p: P |
| 65 | + d: P |
| 66 | + |
| 67 | + |
| 68 | +@dataclass(frozen=True) |
| 69 | +class P: |
| 70 | + x: int |
| 71 | + y: int |
| 72 | + |
| 73 | + def __add__(self, other: P) -> P: |
| 74 | + return P(self.x + other.x, self.y + other.y) |
| 75 | + |
| 76 | + |
| 77 | +CLOCKWISE = frozendict( |
| 78 | + { |
| 79 | + P(1, 0): P(0, 1), |
| 80 | + P(0, 1): P(-1, 0), |
| 81 | + P(-1, 0): P(0, -1), |
| 82 | + P(0, -1): P(1, 0), |
| 83 | + } |
| 84 | +) |
| 85 | + |
| 86 | +ANTICLOCKWISE = frozendict( |
| 87 | + { |
| 88 | + P(1, 0): P(0, -1), |
| 89 | + P(0, 1): P(1, 0), |
| 90 | + P(-1, 0): P(0, 1), |
| 91 | + P(0, -1): P(-1, 0), |
| 92 | + } |
| 93 | +) |
0 commit comments