aoc/year2024/day18.rs
1//! # RAM Run
2//!
3//! We use a trick to speed things up. Instead of storing `#` and `.` in the grid, we store
4//! the time when a block arrives. For example:
5//!
6//! ```none
7//! ...#... ∞ ∞ ∞ 3 ∞ ∞ ∞
8//! 5,4 ..#.... ∞ ∞ 4 ∞ ∞ ∞ ∞
9//! 4,2 ....#.. ∞ ∞ ∞ ∞ 1 ∞ ∞
10//! 4,5 => ....... => ∞ ∞ ∞ ∞ ∞ ∞ ∞
11//! 3,0 .....#. ∞ ∞ ∞ ∞ ∞ 0 ∞
12//! 2,1 ....#.. ∞ ∞ ∞ ∞ 2 ∞ ∞
13//! ....... ∞ ∞ ∞ ∞ ∞ ∞ ∞
14//! ```
15//!
16//! Now we can [BFS](https://en.wikipedia.org/wiki/Breadth-first_search) from any arbitrary
17//! start time. Squares are safe if the grid time is greater than the start time.
18//!
19//! Part two uses an incremental flood fill, getting a little further each time and removing
20//! blocking bytes in descending order of time until we reach the exit.
21//!
22//! * Start with `t = i32::MAX - 1`.
23//! * Start flood fill from top-left origin.
24//! * If we encounter a blocking byte with a time less than `t` then push `(time, position)` onto a
25//! max heap keyed by time.
26//! * If we exhaust the flood fill `VecDeque` then pop the heap's top item. This is the oldest byte
27//! that we encountered blocking the way. Set `t` to the byte's time and push position to the
28//! deque.
29//! * Restart flood fill from new position until we reach the exit.
30use std::cmp::Reverse;
31use std::collections::VecDeque;
32
33use crate::util::grid::*;
34use crate::util::heap::*;
35use crate::util::iter::*;
36use crate::util::parse::*;
37use crate::util::point::*;
38
39pub fn parse(input: &str) -> Grid<u32> {
40 let mut grid = Grid::new(71, 71, u32::MAX);
41
42 for ([x, y], i) in input.iter_signed().chunk::<2>().zip(0..) {
43 grid[Point::new(x, y)] = i;
44 }
45
46 grid
47}
48
49/// BFS from start to exit using a fixed time of 1024.
50pub fn part1(grid: &Grid<u32>) -> u32 {
51 let mut grid = grid.clone();
52 let mut todo = VecDeque::from([(ORIGIN, 0)]);
53
54 grid[ORIGIN] = 0;
55
56 while let Some((position, cost)) = todo.pop_front() {
57 if position == Point::new(70, 70) {
58 return cost;
59 }
60
61 for next in ORTHOGONAL.map(|o| position + o) {
62 if grid.contains(next) && grid[next] > 1024 {
63 grid[next] = 0;
64 todo.push_back((next, cost + 1));
65 }
66 }
67 }
68
69 unreachable!()
70}
71
72/// Incremental flood fill that removes one blocking byte at a time in descending order.
73pub fn part2(grid: &Grid<u32>) -> String {
74 let exit = Point::new(70, 70);
75
76 let mut time = u32::MAX - 1;
77 let mut last = ORIGIN;
78 let mut grid = grid.clone();
79 let mut todo = VecDeque::from([ORIGIN]);
80 let mut heap = MinHeap::new();
81
82 grid[ORIGIN] = 0;
83
84 loop {
85 // Incremental flood fill that makes as much progress as possible.
86 while let Some(position) = todo.pop_front() {
87 if position == exit {
88 return format!("{},{}", last.x, last.y);
89 }
90
91 for next in ORTHOGONAL.map(|o| position + o) {
92 if grid.contains(next) {
93 if time < grid[next] {
94 grid[next] = 0;
95 todo.push_back(next);
96 } else {
97 heap.push(Reverse(grid[next]), next);
98 }
99 }
100 }
101 }
102
103 // Remove the latest blocking byte then try to make a little more progress in flood fill.
104 (Reverse(time), last) = heap.pop().unwrap();
105 todo.push_back(last);
106 }
107}