Skip to content

Commit 020a0d7

Browse files
committed
chore: y2025::day_04
1 parent a3d1232 commit 020a0d7

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

aoclp/src/positioning/pt.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use std::collections::{HashMap, HashSet};
22
use std::convert::Infallible;
33
use std::fmt::{Debug, Display, Formatter};
44
use std::hash::Hash;
@@ -197,3 +197,30 @@ where
197197
})
198198
.collect()
199199
}
200+
201+
/// Given a two-dimensional matrix of elements, returns a map of
202+
/// [`Pt`] associated with the element at that position in the matrix,
203+
/// ignoring any element that are in `skips`.
204+
pub fn filtered_matrix_to_map<M, R, T, PT, S>(matrix: M, skips: S) -> HashMap<Pt<PT>, T>
205+
where
206+
M: IntoIterator<Item = R>,
207+
R: IntoIterator<Item = T>,
208+
PT: TryFrom<usize>,
209+
<PT as TryFrom<usize>>::Error: Debug,
210+
Pt<PT>: Hash + Eq,
211+
S: IntoIterator<Item = T>,
212+
T: Hash + Eq,
213+
{
214+
let skips: HashSet<_> = skips.into_iter().collect();
215+
216+
matrix
217+
.into_iter()
218+
.enumerate()
219+
.flat_map(|(y, row)| {
220+
row.into_iter()
221+
.enumerate()
222+
.filter(|(_, t)| !skips.contains(t))
223+
.map(move |(x, t)| (Pt::new(x.try_into().unwrap(), y.try_into().unwrap()), t))
224+
})
225+
.collect()
226+
}
Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
1+
use std::collections::{BTreeSet, HashSet};
2+
3+
use aoclp::positioning::pt::{filtered_matrix_to_map, Pt};
4+
use aoclp::solvers_impl::input::safe_get_input_as_terrain;
5+
16
pub fn part_1() -> usize {
2-
0
7+
let rolls: HashSet<Pt> = filtered_matrix_to_map(input(), ['.']).into_keys().collect();
8+
9+
rolls
10+
.iter()
11+
.filter(|p| p.eight_neighbours().filter(|n| rolls.contains(n)).count() < 4)
12+
.count()
313
}
414

515
pub fn part_2() -> usize {
6-
0
16+
let mut rolls: BTreeSet<Pt> = filtered_matrix_to_map(input(), ['.']).into_keys().collect();
17+
18+
let mut removed = 0;
19+
loop {
20+
let removables: BTreeSet<_> = rolls
21+
.iter()
22+
.filter(|p| p.eight_neighbours().filter(|n| rolls.contains(n)).count() < 4)
23+
.copied()
24+
.collect();
25+
if removables.is_empty() {
26+
break;
27+
}
28+
29+
removed += removables.len();
30+
rolls = rolls.difference(&removables).copied().collect();
31+
}
32+
33+
removed
34+
}
35+
36+
fn input() -> Vec<Vec<char>> {
37+
safe_get_input_as_terrain(2025, 4)
738
}

0 commit comments

Comments
 (0)