1use std::collections::VecDeque;
16
17use crate::util::grid::*;
18use crate::util::hash::*;
19use crate::util::point::*;
20
21type Key = ((u8, u8), Kind);
22
23#[derive(Clone, Copy, Eq, Hash, PartialEq)]
24enum Kind {
25 Inner,
26 Outer,
27 Start,
28 End,
29}
30
31enum Tile {
32 Wall,
33 Open,
34 Portal(Key, Kind),
35}
36
37struct Edge {
38 to: usize,
39 kind: Kind,
40 distance: u32,
41}
42
43pub struct Maze {
44 start: usize,
45 portals: Vec<Vec<Edge>>,
46}
47
48pub fn parse(input: &str) -> Maze {
51 let grid = Grid::parse(input);
52 let width = grid.width as usize;
53
54 let mut tiles: Vec<_> =
55 grid.bytes.iter().map(|&b| if b == b'.' { Tile::Open } else { Tile::Wall }).collect();
56 let mut map = FastMap::new();
57 let mut found = Vec::new();
58
59 for y in (1..grid.height - 1).step_by(2) {
61 for x in (1..grid.width - 1).step_by(2) {
62 let point = Point::new(x, y);
63 if !grid[point].is_ascii_uppercase() {
64 continue;
65 }
66
67 let (first, second, third) = if grid[point + UP] == b'.' {
69 (point, point + DOWN, point + UP)
70 } else if grid[point + DOWN] == b'.' {
71 (point + UP, point, point + DOWN)
72 } else if grid[point + LEFT] == b'.' {
73 (point, point + RIGHT, point + LEFT)
74 } else if grid[point + RIGHT] == b'.' {
75 (point + LEFT, point, point + RIGHT)
76 } else {
77 continue;
78 };
79
80 let pair = (grid[first], grid[second]);
81 let index = (grid.width * third.y + third.x) as usize;
82 let inner = 2 < x && x < grid.width - 3 && 2 < y && y < grid.height - 3;
83
84 let (kind, opposite) = if inner {
85 (Kind::Inner, Kind::Outer)
86 } else {
87 match pair {
88 (b'A', b'A') => (Kind::Start, Kind::Start),
89 (b'Z', b'Z') => (Kind::End, Kind::End),
90 _ => (Kind::Outer, Kind::Inner),
91 }
92 };
93
94 tiles[index] = Tile::Portal((pair, opposite), kind);
96 map.insert((pair, kind), found.len());
97 found.push(index);
98 }
99 }
100
101 let mut portals = Vec::new();
103 let mut todo = VecDeque::new();
104 let mut seen = vec![0; tiles.len()];
105
106 for start in found {
107 let mut edges = Vec::new();
108 todo.push_back((start, 0));
109
110 while let Some((index, steps)) = todo.pop_front() {
111 seen[index] = start;
112
113 for next_index in [index + 1, index - 1, index + width, index - width] {
114 let next_steps = steps + 1;
115
116 if seen[next_index] != start {
117 match tiles[next_index] {
118 Tile::Wall => (),
119 Tile::Open => todo.push_back((next_index, next_steps)),
120 Tile::Portal(key, kind) => {
121 let to = map[&key];
122 edges.push(Edge { to, kind, distance: next_steps });
123 }
124 }
125 }
126 }
127 }
128
129 portals.push(edges);
130 }
131
132 Maze { start: map[&((b'A', b'A'), Kind::Start)], portals }
133}
134
135pub fn part1(input: &Maze) -> u32 {
137 let mut todo = VecDeque::from([(0, input.start)]);
138
139 while let Some((steps, index)) = todo.pop_front() {
140 for &Edge { to, kind, distance } in &input.portals[index] {
141 let next_steps = steps + distance + 1;
142
143 match kind {
144 Kind::Inner | Kind::Outer => todo.push_back((next_steps, to)),
145 Kind::End => return next_steps - 1,
146 Kind::Start => (),
147 }
148 }
149 }
150
151 unreachable!()
152}
153
154pub fn part2(input: &Maze) -> u32 {
156 let mut cache = FastMap::with_capacity(2_000);
157 let mut todo = VecDeque::from([(0, input.start, 0)]);
158
159 while let Some((steps, index, level)) = todo.pop_front() {
160 let best = cache.entry((index, level)).or_insert(u32::MAX);
161 if *best <= steps {
162 continue;
163 }
164 *best = steps;
165
166 for &Edge { to, kind, distance } in &input.portals[index] {
167 let next_steps = steps + distance + 1;
168
169 match kind {
170 Kind::Inner if level < input.portals.len() => {
172 todo.push_back((next_steps, to, level + 1));
173 }
174 Kind::Outer if level > 0 => {
175 todo.push_back((next_steps, to, level - 1));
176 }
177 Kind::End if level == 0 => {
178 return next_steps - 1;
179 }
180 _ => (),
181 }
182 }
183 }
184
185 unreachable!()
186}