Skip to content

Commit 73fdb88

Browse files
committed
chore: make filtered_matrix_to_map more flexible by accepting filter fn
1 parent 953c496 commit 73fdb88

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

aoclp/src/positioning/pt.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::{HashMap, HashSet};
1+
use std::collections::HashMap;
22
use std::convert::Infallible;
33
use std::fmt::{Debug, Display, Formatter};
44
use std::hash::Hash;
@@ -179,48 +179,38 @@ where
179179

180180
/// Given a two-dimensional matrix of elements, returns a map of
181181
/// [`Pt`] associated with the element at that position in the matrix.
182-
pub fn matrix_to_map<M, R, T, PT>(matrix: M) -> HashMap<Pt<PT>, T>
182+
pub fn matrix_to_map<T, M, R, PT>(matrix: M) -> HashMap<Pt<PT>, T>
183183
where
184184
M: IntoIterator<Item = R>,
185185
R: IntoIterator<Item = T>,
186186
PT: TryFrom<usize>,
187187
<PT as TryFrom<usize>>::Error: Debug,
188-
Pt<PT>: Hash + Eq,
188+
Pt<PT>: Copy + Hash + Eq,
189189
{
190-
matrix
191-
.into_iter()
192-
.enumerate()
193-
.flat_map(|(y, row)| {
194-
row.into_iter()
195-
.enumerate()
196-
.map(move |(x, t)| (Pt::new(x.try_into().unwrap(), y.try_into().unwrap()), t))
197-
})
198-
.collect()
190+
filtered_matrix_to_map(matrix, |_, _| true)
199191
}
200192

201193
/// 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>
194+
/// [`Pt`] associated with the element at that position in the matrix
195+
/// for all elements for which the predicate `f` returns `true`.
196+
pub fn filtered_matrix_to_map<T, M, R, PT, F>(matrix: M, f: F) -> HashMap<Pt<PT>, T>
205197
where
206198
M: IntoIterator<Item = R>,
207199
R: IntoIterator<Item = T>,
208200
PT: TryFrom<usize>,
209201
<PT as TryFrom<usize>>::Error: Debug,
210-
Pt<PT>: Hash + Eq,
211-
S: IntoIterator<Item = T>,
212-
T: Hash + Eq,
202+
Pt<PT>: Copy + Hash + Eq,
203+
F: FnMut(Pt<PT>, &T) -> bool + Clone,
213204
{
214-
let skips: HashSet<_> = skips.into_iter().collect();
215-
216205
matrix
217206
.into_iter()
218207
.enumerate()
219208
.flat_map(|(y, row)| {
209+
let mut f = f.clone();
220210
row.into_iter()
221211
.enumerate()
222-
.filter(|(_, t)| !skips.contains(t))
223212
.map(move |(x, t)| (Pt::new(x.try_into().unwrap(), y.try_into().unwrap()), t))
213+
.filter(move |(pt, t)| f(*pt, t))
224214
})
225215
.collect()
226216
}

aoclp_solutions/src/y2025/day_04.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use aoclp::positioning::pt::{filtered_matrix_to_map, Pt};
44
use aoclp::solvers_impl::input::safe_get_input_as_terrain;
55

66
pub fn part_1() -> usize {
7-
let rolls: HashSet<Pt> = filtered_matrix_to_map(input(), ['.']).into_keys().collect();
7+
let rolls: HashSet<Pt> = filtered_matrix_to_map(input(), |_, &c| c != '.')
8+
.into_keys()
9+
.collect();
810

911
rolls
1012
.iter()
@@ -13,7 +15,9 @@ pub fn part_1() -> usize {
1315
}
1416

1517
pub fn part_2() -> usize {
16-
let mut rolls: BTreeSet<Pt> = filtered_matrix_to_map(input(), ['.']).into_keys().collect();
18+
let mut rolls: BTreeSet<Pt> = filtered_matrix_to_map(input(), |_, &c| c != '.')
19+
.into_keys()
20+
.collect();
1721

1822
let mut removed = 0;
1923
loop {

0 commit comments

Comments
 (0)