Skip to content

Commit fe6d72c

Browse files
committed
Year 2024 Day 10
1 parent e5bfc16 commit fe6d72c

File tree

7 files changed

+107
-0
lines changed

7 files changed

+107
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
8181
| 7 | [Bridge Repair](https://adventofcode.com/2024/day/7) | [Source](src/year2024/day07.rs) | 136 |
8282
| 8 | [Resonant Collinearity](https://adventofcode.com/2024/day/8) | [Source](src/year2024/day08.rs) | 8 |
8383
| 9 | [Disk Fragmenter](https://adventofcode.com/2024/day/9) | [Source](src/year2024/day09.rs) | 163 |
84+
| 10 | [Hoof It](https://adventofcode.com/2024/day/10) | [Source](src/year2024/day10.rs) | 41 |
8485

8586
## 2023
8687

benches/benchmark.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,5 @@ mod year2024 {
301301
benchmark!(year2024, day07);
302302
benchmark!(year2024, day08);
303303
benchmark!(year2024, day09);
304+
benchmark!(year2024, day10);
304305
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,4 +300,5 @@ pub mod year2024 {
300300
pub mod day07;
301301
pub mod day08;
302302
pub mod day09;
303+
pub mod day10;
303304
}

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,5 +370,6 @@ fn year2024() -> Vec<Solution> {
370370
solution!(year2024, day07),
371371
solution!(year2024, day08),
372372
solution!(year2024, day09),
373+
solution!(year2024, day10),
373374
]
374375
}

src/year2024/day10.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//! # Hoof It
2+
//!
3+
//! [Breadth first search](https://en.wikipedia.org/wiki/Breadth-first_search) for both parts.
4+
//! Part two is simpler than part one as we don't need to keep track of already visited points.
5+
//! My input contained fewer peaks `9` than valleys `0`` so it was slightly faster
6+
//! to reverse search.
7+
use crate::util::grid::*;
8+
use crate::util::point::*;
9+
use std::collections::VecDeque;
10+
11+
pub fn parse(input: &str) -> Grid<u8> {
12+
Grid::parse(input)
13+
}
14+
15+
pub fn part1(grid: &Grid<u8>) -> u32 {
16+
let mut starts = Vec::new();
17+
18+
for y in 0..grid.height {
19+
for x in 0..grid.width {
20+
let point = Point::new(x, y);
21+
if grid[point] == b'9' {
22+
starts.push(point);
23+
}
24+
}
25+
}
26+
27+
let mut todo = VecDeque::new();
28+
let mut seen = grid.same_size_with(usize::MAX);
29+
let mut result = 0;
30+
31+
for (index, &start) in starts.iter().enumerate() {
32+
todo.push_back(start);
33+
34+
while let Some(point) = todo.pop_front() {
35+
for next in ORTHOGONAL.map(|o| point + o) {
36+
if grid.contains(next) && grid[next] + 1 == grid[point] && seen[next] != index {
37+
seen[next] = index;
38+
39+
if grid[next] == b'0' {
40+
result += 1;
41+
} else {
42+
todo.push_back(next);
43+
}
44+
}
45+
}
46+
}
47+
}
48+
49+
result
50+
}
51+
52+
pub fn part2(grid: &Grid<u8>) -> u32 {
53+
let mut todo = VecDeque::new();
54+
55+
for y in 0..grid.height {
56+
for x in 0..grid.width {
57+
let start = Point::new(x, y);
58+
if grid[start] == b'9' {
59+
todo.push_back(start);
60+
}
61+
}
62+
}
63+
64+
let mut result = 0;
65+
66+
while let Some(point) = todo.pop_front() {
67+
for next in ORTHOGONAL.map(|o| point + o) {
68+
if grid.contains(next) && grid[next] + 1 == grid[point] {
69+
if grid[next] == b'0' {
70+
result += 1;
71+
} else {
72+
todo.push_back(next);
73+
}
74+
}
75+
}
76+
}
77+
78+
result
79+
}

tests/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,5 @@ mod year2024 {
290290
mod day07_test;
291291
mod day08_test;
292292
mod day09_test;
293+
mod day10_test;
293294
}

tests/year2024/day10_test.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use aoc::year2024::day10::*;
2+
3+
const EXAMPLE: &str = "\
4+
89010123
5+
78121874
6+
87430965
7+
96549874
8+
45678903
9+
32019012
10+
01329801
11+
10456732";
12+
13+
#[test]
14+
fn part1_test() {
15+
let input = parse(EXAMPLE);
16+
assert_eq!(part1(&input), 36);
17+
}
18+
19+
#[test]
20+
fn part2_test() {
21+
let input = parse(EXAMPLE);
22+
assert_eq!(part2(&input), 81);
23+
}

0 commit comments

Comments
 (0)