From 822fbc0ecd51b1c7da32b31a0ffc23d09a06c7c6 Mon Sep 17 00:00:00 2001 From: Guillermo Ramos Date: Fri, 6 Dec 2024 17:55:41 +0100 Subject: 2024.6 --- 2024_rust/src/lib.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to '2024_rust/src/lib.rs') 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 { + dots: Vec>, + pub limit: Pos, + } + + impl Matrix { + pub fn new(text: &str, parse: F) -> Self + where + F: Fn(char) -> T, + { + let dots: Vec> = 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 fmt::Display for Matrix + 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::() + )?; + } + fmt::Result::Ok(()) + } + } +} + pub fn run_day(day: &str, p1: S1, p2: S2) where S1: FnOnce(&str) -> String, -- cgit v1.2.3