-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23.py
172 lines (136 loc) · 4.64 KB
/
day23.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from __future__ import annotations
from lib import Point
from enum import Enum, auto
from collections import defaultdict
from dataclasses import dataclass
class Direction(Enum):
Up = auto()
Down = auto()
Left = auto()
Right = auto()
def move(self) -> Point:
match self:
case Direction.Up: return Point(0, -1)
case Direction.Down: return Point(0, 1)
case Direction.Left: return Point(-1, 0)
case Direction.Right: return Point(1, 0)
def all() -> list[Direction]:
return [Direction.Up, Direction.Down, Direction.Left, Direction.Right]
class Tile(Enum):
Open = auto()
Forest = auto()
SlopeUp = auto()
SlopeDown = auto()
SlopeLeft = auto()
SlopeRight = auto()
def parse(ch: str) -> Tile | None:
match ch:
case ".": return Tile.Open
case "^": return Tile.SlopeUp
case "v": return Tile.SlopeDown
case "<": return Tile.SlopeLeft
case ">": return Tile.SlopeRight
case _: return None
@dataclass(frozen=True)
class State:
start: Point
finish: Point
graph: dict[Point, set[tuple[Point, int]]]
@classmethod
def parse(cls, input: str) -> State:
map = {
Point(x, y): tile
for y, line in enumerate(input.splitlines())
for x, ch in enumerate(line)
if (tile := Tile.parse(ch))
}
keys = list(map.keys())
return cls(keys[0], keys[-1], cls.make_graph(map))
def make_graph(map: dict[Point, Tile]) -> dict[Point, set[tuple[Point, int]]]:
raise NotImplementedError()
def solve(self) -> int:
queue, answer = [(self.start, {self.start}, 0)], 0
while queue:
pos, seen, steps = queue.pop()
if pos == self.finish:
if steps > answer:
answer = steps
continue
seen.add(pos)
queue.extend(
(np, seen | {pos}, steps + cost)
for np, cost in self.graph[pos]
if np not in seen
)
return answer
class StateSlippery(State):
def make_graph(map: dict[Point, Tile]) -> dict[Point, set[tuple[Point, int]]]:
def can_traverse(tile: Tile, dir: Direction) -> bool:
match (tile, dir):
case (Tile.Open, _): return True
case (Tile.SlopeUp, Direction.Up): return True
case (Tile.SlopeDown, Direction.Down): return True
case (Tile.SlopeLeft, Direction.Left): return True
case (Tile.SlopeRight, Direction.Right): return True
case _: return False
return {
pos: {
(np, 1) for dir in Direction.all()
if (np := pos + dir.move()) in map and can_traverse(tile, dir)
} for pos, tile in map.items()
}
class StateNormal(State):
def make_graph(map: dict[Point, Tile]) -> dict[Point, set[tuple[Point, int]]]:
raw_graph = {
pos: {np for dir in Direction.all() if (np := pos + dir.move()) in map}
for pos in map
}
stack = [list(map.keys())[0]]
seen = set()
sparse_graph = defaultdict(set)
while stack and (node := stack.pop()):
seen.add(node)
for next_node in raw_graph[node]:
if next_node not in seen:
cost = 1
while next_node not in seen and len(raw_graph[next_node]) == 2:
seen.add(next_node)
next_node = next(n for n in raw_graph[next_node] if n not in seen)
cost += 1
stack.append(next_node)
sparse_graph[node].add((next_node, cost))
sparse_graph[next_node].add((node, cost))
return sparse_graph
def part1(input: str) -> int:
return StateSlippery.parse(input).solve()
def part2(input: str) -> int:
return StateNormal.parse(input).solve()
TEST_INPUT = """#.#####################
#.......#########...###
#######.#########.#.###
###.....#.>.>.###.#.###
###v#####.#v#.###.#.###
###.>...#.#.#.....#...#
###v###.#.#.#########.#
###...#.#.#.......#...#
#####.#.#.#######.#.###
#.....#.#.#.......#...#
#.#####.#.#.#########v#
#.#...#...#...###...>.#
#.#.#v#######v###.###v#
#...#.>.#...>.>.#.###.#
#####v#.#.###v#.#.###.#
#.....#...#...#.#.#...#
#.#########.###.#.#.###
#...###...#...#...#.###
###.###.#.###v#####v###
#...#...#.#.>.>.#.>.###
#.###.###.#.###.#.#v###
#.....###...###...#...#
#####################.#"""
PART1_TESTS = [
(TEST_INPUT, 94),
]
PART2_TESTS = [
(TEST_INPUT, 154),
]