Skip to content

Commit 9b8c817

Browse files
committed
Year 2024 Day 18
1 parent 2834fe0 commit 9b8c817

File tree

7 files changed

+81
-4
lines changed

7 files changed

+81
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
8989
| 15 | [Warehouse Woes](https://adventofcode.com/2024/day/15) | [Source](src/year2024/day15.rs) | 303 |
9090
| 16 | [Reindeer Maze](https://adventofcode.com/2024/day/16) | [Source](src/year2024/day16.rs) | 390 |
9191
| 17 | [Chronospatial Computer](https://adventofcode.com/2024/day/17) | [Source](src/year2024/day17.rs) | 2 |
92+
| 18 | [RAM Run](https://adventofcode.com/2024/day/18) | [Source](src/year2024/day18.rs) | 90 |
9293

9394
## 2023
9495

benches/benchmark.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,5 @@ benchmark!(year2023
8888

8989
benchmark!(year2024
9090
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13,
91-
day14, day15, day16, day17
91+
day14, day15, day16, day17, day18
9292
);

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,5 @@ library!(year2023 "Restore global snow production."
6868

6969
library!(year2024 "Locate the Chief Historian in time for the big Christmas sleigh launch."
7070
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13,
71-
day14, day15, day16, day17
71+
day14, day15, day16, day17, day18
7272
);

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,5 @@ run!(year2023
142142

143143
run!(year2024
144144
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13,
145-
day14, day15, day16, day17
145+
day14, day15, day16, day17, day18
146146
);

src/year2024/day18.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use crate::util::grid::*;
2+
use crate::util::iter::*;
3+
use crate::util::parse::*;
4+
use crate::util::point::*;
5+
use std::collections::VecDeque;
6+
7+
type Input = (Vec<(usize, Point)>, Grid<usize>);
8+
9+
pub fn parse(input: &str) -> Input {
10+
let points: Vec<_> = input
11+
.iter_signed::<i32>()
12+
.chunk::<2>()
13+
.map(|[x, y]| Point::new(x, y))
14+
.enumerate()
15+
.collect();
16+
17+
let mut grid = Grid::new(71, 71, usize::MAX);
18+
points.iter().for_each(|&(i, p)| grid[p] = i);
19+
20+
(points, grid)
21+
}
22+
23+
pub fn part1(input: &Input) -> u32 {
24+
let (_, grid) = input;
25+
26+
let todo = &mut VecDeque::new();
27+
let seen = &mut grid.same_size_with(usize::MAX);
28+
29+
bfs(grid, todo, seen, 1024).unwrap()
30+
}
31+
32+
pub fn part2(input: &Input) -> String {
33+
let (points, grid) = input;
34+
35+
let todo = &mut VecDeque::new();
36+
let seen = &mut grid.same_size_with(usize::MAX);
37+
38+
let first = points.partition_point(|&(id, _)| bfs(grid, todo, seen, id).is_some());
39+
let blocker = points[first + 1].1;
40+
format!("{},{}", blocker.x, blocker.y)
41+
}
42+
43+
fn bfs(
44+
grid: &Grid<usize>,
45+
todo: &mut VecDeque<(Point, u32)>,
46+
seen: &mut Grid<usize>,
47+
id: usize,
48+
) -> Option<u32> {
49+
todo.clear();
50+
todo.push_back((ORIGIN, 0));
51+
seen[ORIGIN] = id;
52+
53+
while let Some((position, cost)) = todo.pop_front() {
54+
if position == Point::new(70, 70) {
55+
return Some(cost);
56+
}
57+
58+
for next in ORTHOGONAL.map(|o| position + o) {
59+
if grid.contains(next) && grid[next] > id && seen[next] != id {
60+
todo.push_back((next, cost + 1));
61+
seen[next] = id;
62+
}
63+
}
64+
}
65+
66+
None
67+
}

tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,5 @@ test!(year2023
8181

8282
test!(year2024
8383
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13,
84-
day14, day15, day16, day17
84+
day14, day15, day16, day17, day18
8585
);

tests/year2024/day18.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#[test]
2+
fn part1_test() {
3+
// No test
4+
}
5+
6+
#[test]
7+
fn part2_test() {
8+
// No test
9+
}

0 commit comments

Comments
 (0)