diff options
Diffstat (limited to '2024_rust/src/lib.rs')
-rw-r--r-- | 2024_rust/src/lib.rs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/2024_rust/src/lib.rs b/2024_rust/src/lib.rs index f8a84d6..c034fea 100644 --- a/2024_rust/src/lib.rs +++ b/2024_rust/src/lib.rs @@ -1,3 +1,52 @@ +pub mod matrix { + pub type Pos = (usize, usize); + + #[derive(Clone)] + pub struct Matrix<T> { + dots: Vec<Vec<T>>, + pub limit: Pos, + } + + impl<T> Matrix<T> { + pub fn new<F>(text: &str, parse: F) -> Self + where + F: Fn(char) -> T, + { + let dots: Vec<Vec<T>> = text + .lines() + .map(|row| row.chars().map(|c| parse(c)).collect()) + .collect(); + let limit = (dots.len(), dots[0].len()); + Self { dots, limit } + } + + pub fn get(&self, (x, y): Pos) -> &T { + &self.dots[x][y] + } + + pub fn set(&mut self, (x, y): Pos, dot: T) { + self.dots[x][y] = dot; + } + } + + use std::fmt; + impl<T> fmt::Display for Matrix<T> + where + T: fmt::Display, + { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + for row in &self.dots { + write!( + f, + "{}\n", + row.iter().map(|c| c.to_string()).collect::<String>() + )?; + } + fmt::Result::Ok(()) + } + } +} + pub fn run_day<S1, S2>(day: &str, p1: S1, p2: S2) where S1: FnOnce(&str) -> String, |