10000 chore: add neighbours + matrix to map helpers for `Pt` · clechasseur/adventofcode.rs@79d681f · GitHub
[go: up one dir, main page]

Skip to content

Commit 79d681f

Browse files
committed
chore: add neighbours + matrix to map helpers for Pt
1 parent 0174c8e commit 79d681f

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

aoclp/src/positioning/pt.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
use std::fmt::{Display, Formatter};
1+
use std::collections::HashMap;
2+
use std::fmt::{Debug, Display, Formatter};
3+
use std::hash::Hash;
24
use std::ops::{Add, AddAssign, Sub, SubAssign};
35
use std::str::FromStr;
46
use std::sync::OnceLock;
57

8+
use strum::IntoEnumIterator;
9+
610
use crate::captures::CapturesHelper;
711
use crate::num::{zero, Signed, Zero};
12+
use crate::positioning::direction::eight_points::Direction8;
13+
use crate::positioning::direction::four_points::Direction4;
14+
use crate::positioning::direction::MovementDirection;
815
use crate::regex::Regex;
916

1017
/// A point in 2D space.
@@ -20,6 +27,26 @@ impl<T> Pt<T> {
2027
}
2128
}
2229

30+
impl<T> Pt<T>
31+
where
32+
Self: Add<Output = Self> + Copy,
33+
Direction4: MovementDirection<T>,
34+
{
35+
pub fn four_neighbours(self) -> impl Iterator<Item = Self> {
36+
Direction4::iter().map(move |dir| self + dir.displacement())
37+
}
38+
}
39+
40+
impl<T> Pt<T>
41+
where
42+
Self: Add<Output = Self> + Copy,
43+
Direction8: MovementDirection<T>,
44+
{
45+
pub fn eight_neighbours(self) -> impl Iterator<Item = Self> {
46+
Direction8::iter().map(move |dir| self + dir.displacement())
47+
}
48+
}
49+
2350
impl<T, U, V> From<(U, V)> for Pt<T>
2451
where
2552
U: Into<T>,
@@ -135,3 +162,24 @@ where
135162
{
136163
(a.x - b.x).abs() + (a.y - b.y).abs()
137164
}
165+
166+
/// Given a two-dimensional matrix of elements, returns a map of
167+
/// [`Pt`] associated with the element at that position in the matrix.
168+
pub fn matrix_to_map<M, R, T, PT>(matrix: M) -> HashMap<Pt<PT>, T>
169+
where
170+
M: IntoIterator<Item = R>,
171+
R: IntoIterator<Item = T>,
172+
PT: TryFrom<usize>,
173+
<PT as TryFrom<usize>>::Error: Debug,
174+
Pt<PT>: Hash + Eq,
175+
{
176+
matrix
177+
.into_iter()
178+
.enumerate()
179+
.flat_map(|(y, row)| {
180+
row.into_iter()
181+
.enumerate()
182+
.map(move |(x, t)| (Pt::new(x.try_into().unwrap(), y.try_into().unwrap()), t))
183+
})
184+
.collect()
185+
}

aoclp_solutions/src/y2024/day_04.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::collections::HashMap;
22
use std::iter::successors;
33

44
use aoclp::positioning::direction::eight_points::Direction8;
5-
use aoclp::positioning::pt::Pt;
5+
use aoclp::positioning::pt::{matrix_to_map, Pt};
66
use aoclp::solvers_impl::input::safe_get_input_as_terrain;
77
use strum::IntoEnumIterator;
88

@@ -69,17 +69,7 @@ impl WordSearch {
6969

7070
impl From<Vec<Vec<char>>> for WordSearch {
7171
fn from(value: Vec<Vec<char>>) -> Self {
72-
Self(
73-
value
74-
.into_iter()
75-
.enumerate()
76-
.flat_map(|(y, row)| {
77-
row.into_iter()
78-
.enumerate()
79-
.map(move |(x, c)| (Pt::new(x as i64, y as i64), c))
80-
})
81-
.collect(),
82-
)
72+
Self(matrix_to_map(value))
8373
}
8474
}
8575

0 commit comments

Comments
 (0)
0