Skip to content

Commit 1a82e61

Browse files
committed
[2024] Cleanup day 10
1 parent 9f14acb commit 1a82e61

File tree

3 files changed

+25
-54
lines changed

3 files changed

+25
-54
lines changed

aoc_2024/src/day_07.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn solve(input: &str, part_b: bool) -> u64 {
1717
// For part a, we check if its valid using `is_valid` with part_b = false,
1818
// because an equation that is valid for part a is must be valid for part b,
1919
// we can get a small speedup by only doing the more intense part_b = true
20-
// check if needed.
20+
// check if needed.
2121
problem
2222
.cases
2323
.into_iter()
@@ -85,7 +85,7 @@ impl TestCase {
8585
}
8686

8787
// Increments the leftmost operation, carrying if it exceeds 1 for
88-
// part a or 2 for part b.
88+
// part a or 2 for part b.
8989
for op in ops.iter_mut() {
9090
*op += 1;
9191
if *op <= (1 + part_b as usize) {

aoc_2024/src/day_10.rs

Lines changed: 23 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,18 @@ use nd_vec::Vec2;
77
solution!("Hoof It", 10);
88

99
fn part_a(input: &str) -> Answer {
10-
let map = Map::parse(input);
11-
map.trailheads()
12-
.into_iter()
13-
.map(|x| map.score(x))
14-
.sum::<usize>()
15-
.into()
10+
solve(input, false).into()
1611
}
1712

1813
fn part_b(input: &str) -> Answer {
14+
solve(input, true).into()
15+
}
16+
17+
fn solve(input: &str, part_b: bool) -> usize {
1918
let map = Map::parse(input);
2019
map.trailheads()
21-
.into_iter()
22-
.map(|x| map.rating(x))
20+
.map(|x| map.score(x, !part_b))
2321
.sum::<usize>()
24-
.into()
2522
}
2623

2724
struct Map {
@@ -34,15 +31,17 @@ impl Map {
3431
Self { board }
3532
}
3633

37-
fn trailheads(&self) -> Vec<Vec2<usize>> {
34+
// Find the coordinates of all 0s
35+
fn trailheads(&self) -> impl Iterator<Item = Vec2<usize>> + use<'_> {
3836
self.board
3937
.iter()
4038
.filter(|(_, &tile)| tile == 0)
4139
.map(|(pos, _)| pos)
42-
.collect()
4340
}
4441

45-
fn score(&self, pos: Vec2<usize>) -> usize {
42+
// Simple BFS for pathfinding, where we don't avoid going to already
43+
// explored tiles if on part B.
44+
fn score(&self, pos: Vec2<usize>, no_repeats: bool) -> usize {
4645
let mut queue = VecDeque::new();
4746
let mut seen = HashSet::new();
4847

@@ -52,49 +51,22 @@ impl Map {
5251
let mut score = 0;
5352
while let Some(pos) = queue.pop_front() {
5453
let value = *self.board.get(pos).unwrap();
55-
if value == 9 {
56-
score += 1;
57-
}
58-
59-
for dir in Direction::ALL {
60-
if let Some(next) = dir.try_advance(pos) {
61-
if self.board.contains(next)
62-
&& *self.board.get(next).unwrap() == value + 1
63-
&& seen.insert(next)
64-
{
65-
queue.push_back(next);
66-
}
67-
}
68-
}
54+
score += (value == 9) as usize;
55+
56+
queue.extend(
57+
Direction::ALL
58+
.iter()
59+
.filter_map(|&dir| dir.try_advance(pos))
60+
.filter(|&next| {
61+
self.board.contains(next)
62+
&& *self.board.get(next).unwrap() == value + 1
63+
&& (!no_repeats || seen.insert(next))
64+
}),
65+
);
6966
}
7067

7168
score
7269
}
73-
74-
fn rating(&self, pos: Vec2<usize>) -> usize {
75-
fn inner(board: &Matrix<u32>, pos: Vec2<usize>, mut seen: HashSet<Vec2<usize>>) -> usize {
76-
let value = *board.get(pos).unwrap();
77-
if value == 9 {
78-
return 1;
79-
}
80-
81-
let mut sum = 0;
82-
for dir in Direction::ALL {
83-
if let Some(next) = dir.try_advance(pos) {
84-
if board.contains(next)
85-
&& *board.get(next).unwrap() == value + 1
86-
&& seen.insert(next)
87-
{
88-
sum += inner(board, next, seen.clone());
89-
}
90-
}
91-
}
92-
93-
sum
94-
}
95-
96-
inner(&self.board, pos, HashSet::new())
97-
}
9870
}
9971

10072
#[cfg(test)]

common/src/misc.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,3 @@ pub fn load_raw(year: u16, day: u32) -> io::Result<String> {
1111
let file = format!("data/{year}/{:02}.txt", day);
1212
fs::read_to_string(file)
1313
}
14-

0 commit comments

Comments
 (0)