Skip to content

Commit

Permalink
Year 2024 Day 18
Browse files Browse the repository at this point in the history
  • Loading branch information
maneatingape committed Dec 18, 2024
1 parent 2834fe0 commit 15739f5
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
| 15 | [Warehouse Woes](https://adventofcode.com/2024/day/15) | [Source](src/year2024/day15.rs) | 303 |
| 16 | [Reindeer Maze](https://adventofcode.com/2024/day/16) | [Source](src/year2024/day16.rs) | 390 |
| 17 | [Chronospatial Computer](https://adventofcode.com/2024/day/17) | [Source](src/year2024/day17.rs) | 2 |
| 18 | [RAM Run](https://adventofcode.com/2024/day/18) | [Source](src/year2024/day18.rs) | 61 |

## 2023

Expand Down
2 changes: 1 addition & 1 deletion benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,5 @@ benchmark!(year2023

benchmark!(year2024
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13,
day14, day15, day16, day17
day14, day15, day16, day17, day18
);
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ library!(year2023 "Restore global snow production."

library!(year2024 "Locate the Chief Historian in time for the big Christmas sleigh launch."
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13,
day14, day15, day16, day17
day14, day15, day16, day17, day18
);
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,5 @@ run!(year2023

run!(year2024
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13,
day14, day15, day16, day17
day14, day15, day16, day17, day18
);
79 changes: 79 additions & 0 deletions src/year2024/day18.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//! # RAM Run
//!
//! We use a trick to speed things up. Instead of storing `#` and `.` in the grid, we store
//! the time when a block arrives. For example:
//!
//! ```none
//! ...#... ∞ ∞ ∞ 3 ∞ ∞ ∞
//! 5,4 ..#.... ∞ ∞ 4 ∞ ∞ ∞ ∞
//! 4,2 ....#.. ∞ ∞ ∞ ∞ 1 ∞ ∞
//! 4,5 => ....... => ∞ ∞ ∞ ∞ ∞ ∞ ∞
//! 3,0 .....#. ∞ ∞ ∞ ∞ ∞ 0 ∞
//! 2,1 ....#.. ∞ ∞ ∞ ∞ 2 ∞ ∞
//! ....... ∞ ∞ ∞ ∞ ∞ ∞ ∞
//! ```
//!
//! Now we can [BFS](https://en.wikipedia.org/wiki/Breadth-first_search) from any arbitrary
//! start time. Squares are blocked only if the grid time is less than or equal to the start time.
//!
//! A [binary search](https://en.wikipedia.org/wiki/Binary_search) is much faster than a
//! linear search with complexity `O(log₂n)` vs `O(n)`. For example `log₂(3450) = 12`.
use crate::util::grid::*;
use crate::util::iter::*;
use crate::util::parse::*;
use crate::util::point::*;
use std::collections::VecDeque;

pub fn parse(input: &str) -> Grid<u16> {
let mut grid = Grid::new(71, 71, u16::MAX);

for (i, [x, y]) in input.iter_signed::<i32>().chunk::<2>().enumerate() {
grid[Point::new(x, y)] = i as u16;
}

grid
}

pub fn part1(grid: &Grid<u16>) -> u32 {
bfs(grid, 1024).unwrap()
}

pub fn part2(grid: &Grid<u16>) -> String {
let mut lower = 0;
let mut upper = 5041;

while lower < upper {
let middle = (lower + upper) / 2;
if bfs(grid, middle).is_some() {
lower = middle + 1;
} else {
upper = middle;
}
}

let index = grid.bytes.iter().position(|&time| time == lower).unwrap() as i32;
format!("{},{}", index % grid.width, index / grid.width)
}

fn bfs(grid: &Grid<u16>, time: u16) -> Option<u32> {
let mut todo = VecDeque::new();
let mut seen = grid.clone();

todo.push_back((ORIGIN, 0));
seen[ORIGIN] = 0;

while let Some((position, cost)) = todo.pop_front() {
if position == Point::new(70, 70) {
return Some(cost);
}

for next in ORTHOGONAL.map(|o| position + o) {
if seen.contains(next) && time < seen[next] {
todo.push_back((next, cost + 1));
seen[next] = 0;
}
}
}

None
}
2 changes: 1 addition & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,5 @@ test!(year2023

test!(year2024
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13,
day14, day15, day16, day17
day14, day15, day16, day17, day18
);
9 changes: 9 additions & 0 deletions tests/year2024/day18.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[test]
fn part1_test() {
// No test
}

#[test]
fn part2_test() {
// No test
}

0 comments on commit 15739f5

Please sign in to comment.