-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16.rs
320 lines (264 loc) · 8.01 KB
/
day16.rs
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//! [Day 16: Reindeer Maze](https://adventofcode.com/2024/day/16)
use rustc_hash::{FxHashMap, FxHashSet};
use std::collections::BinaryHeap;
use aoc::Coord;
const ZERO: Coord = Coord { x: 0, y: 0 };
const EAST: Coord = Coord { x: 1, y: 0 }; // starting direction
struct Cost1 {
cost: u32,
pos: Coord,
dir: Coord,
}
impl Cost1 {
const fn new(cost: u32, pos: Coord, dir: Coord) -> Self {
Self { cost, pos, dir }
}
}
impl Ord for Cost1 {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
other.cost.cmp(&self.cost)
}
}
impl PartialOrd for Cost1 {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for Cost1 {
fn eq(&self, other: &Self) -> bool {
self.cost == other.cost
}
}
impl Eq for Cost1 {}
struct Cost2 {
cost: u32,
pos: Coord,
dir: Coord,
path: Vec<Coord>,
}
impl Cost2 {
const fn new(cost: u32, pos: Coord, dir: Coord, path: Vec<Coord>) -> Self {
Self {
cost,
pos,
dir,
path,
}
}
}
impl Ord for Cost2 {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
other.cost.cmp(&self.cost)
}
}
impl PartialOrd for Cost2 {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for Cost2 {
fn eq(&self, other: &Self) -> bool {
self.cost == other.cost
}
}
impl Eq for Cost2 {}
struct Puzzle {
start: Coord,
end: Coord,
maze: FxHashSet<Coord>,
size: Coord,
}
impl Puzzle {
fn new(data: &str) -> Self {
let mut start = ZERO;
let mut end = ZERO;
let mut maze = FxHashSet::default();
let mut size = ZERO;
for (y, line) in data.lines().enumerate() {
let y = i32::try_from(y).unwrap();
for (x, c) in line.chars().enumerate() {
let x = i32::try_from(x).unwrap();
if c == '#' {
continue;
}
if c == 'S' {
start = Coord::new(x, y);
} else if c == 'E' {
end = Coord::new(x, y);
}
maze.insert(Coord { x, y });
size.x = x;
}
size.y = y;
}
Self {
start,
end,
maze,
size,
}
}
#[cfg(feature = "anim")]
fn show_maze(&self, path: &[Coord], n: u32) {
const SCALE: u32 = 2;
let width = self.size.x as u32 + 1;
let height = self.size.y as u32 + 1;
let mut imgbuf = image::ImageBuffer::new(width * SCALE, height * SCALE);
// Iterate over the coordinates and pixels of the image
for (x, y, pixel) in imgbuf.enumerate_pixels_mut() {
let r = (0.3 * x as f32) as u8;
let b = (0.3 * y as f32) as u8;
*pixel = image::Rgb([r, 0, b]);
}
for y in 0..=self.size.y {
for x in 0..=self.size.x {
let pos = Coord { x, y };
let c = image::Rgb(if pos == self.start {
[0, 255, 0]
} else if pos == self.end {
[255, 255, 0]
} else if path.contains(&pos) {
[0, 0, 255]
} else if self.maze.contains(&pos) {
continue;
} else {
[0, 0, 0]
});
let x = x as u32 * SCALE;
let y = y as u32 * SCALE;
for k in 0..(SCALE * SCALE) {
let pixel = imgbuf.get_pixel_mut(x + k % SCALE, y + k / SCALE);
*pixel = c
}
}
imgbuf.save(format!("frame{n:05}.png")).unwrap();
}
}
#[allow(dead_code)]
fn show_maze_ascii(&self, path: &[Coord]) {
for y in 0..=self.size.y {
for x in 0..=self.size.x {
let pos = Coord { x, y };
let c = if pos == self.start {
'S'
} else if pos == self.end {
'E'
} else if path.contains(&pos) {
'O'
} else if self.maze.contains(&pos) {
'.'
} else {
'#'
};
print!("{c}");
}
println!();
}
}
/// Solve part one.
fn part1(&self) -> u32 {
let mut seen = FxHashSet::default();
let mut heap = BinaryHeap::new();
heap.push(Cost1::new(0, self.start, EAST));
while let Some(Cost1 { cost, pos, dir }) = heap.pop() {
seen.insert((pos, dir));
let counterclockwise = Coord::new(dir.y, -dir.x);
let clockwise = Coord::new(-dir.y, dir.x);
for (new_cost, new_pos, new_dir) in [
(cost + 1, pos + dir, dir), // advance in same direction
(cost + 1001, pos + counterclockwise, counterclockwise), // turn then move
(cost + 1001, pos + clockwise, clockwise), // turn then move
] {
if new_pos == self.end {
return new_cost;
}
if self.maze.contains(&new_pos) && !seen.contains(&(new_pos, dir)) {
heap.push(Cost1::new(new_cost, new_pos, new_dir));
}
}
}
0
}
/// Solve part two.
fn part2(&self) -> usize {
let mut heap = BinaryHeap::new();
let mut costs = FxHashMap::default();
let mut best_path_tiles: FxHashSet<Coord> = FxHashSet::default();
let mut best_cost = u32::MAX;
#[cfg(feature = "anim")]
let mut frames = 0;
heap.push(Cost2::new(0, self.start, EAST, [self.start].to_vec()));
while let Some(Cost2 {
cost,
pos,
dir,
path: tiles,
}) = heap.pop()
{
if pos == self.end {
best_cost = best_cost.min(cost);
if best_cost == cost {
best_path_tiles.extend(&tiles);
#[cfg(feature = "anim")]
{
self.show_maze(&tiles, frames);
frames += 1;
}
}
}
let counterclockwise = Coord::new(dir.y, -dir.x);
let clockwise = Coord::new(-dir.y, dir.x);
for (new_cost, new_pos, new_dir) in [
(cost + 1, pos + dir, dir),
(cost + 1001, pos + counterclockwise, counterclockwise),
(cost + 1001, pos + clockwise, clockwise),
] {
if self.maze.contains(&new_pos)
&& costs.get(&(new_pos, new_dir)).copied().unwrap_or(u32::MAX) >= new_cost
{
costs.insert((new_pos, new_dir), new_cost);
let mut tiles = tiles.clone();
tiles.push(new_pos);
heap.push(Cost2::new(new_cost, new_pos, new_dir, tiles));
}
}
}
best_path_tiles.len()
}
}
/// # Panics
#[must_use]
pub fn solve(data: &str) -> (u32, usize) {
let puzzle = Puzzle::new(data);
(puzzle.part1(), puzzle.part2())
}
pub fn main() {
let args = aoc::parse_args();
args.run(solve);
}
#[cfg(test)]
mod test {
use super::*;
const SAMPLE_1: &str = include_str!("sample_1.txt");
const SAMPLE_3: &str = include_str!("sample_3.txt");
#[test]
fn test01() {
let puzzle = Puzzle::new(SAMPLE_1);
assert_eq!(puzzle.part1(), 7036);
}
#[test]
fn test02() {
let puzzle = Puzzle::new(SAMPLE_3);
assert_eq!(puzzle.part1(), 11048);
}
#[test]
fn test03() {
let puzzle = Puzzle::new(SAMPLE_1);
assert_eq!(puzzle.part2(), 45);
}
#[test]
fn test04() {
let puzzle = Puzzle::new(SAMPLE_3);
assert_eq!(puzzle.part2(), 64);
}
}