summaryrefslogtreecommitdiff
path: root/2024_rust/src/lib.rs
diff options
context:
space:
mode:
authorGuillermo Ramos2024-12-06 17:55:41 +0100
committerGuillermo Ramos2024-12-07 00:44:00 +0100
commit822fbc0ecd51b1c7da32b31a0ffc23d09a06c7c6 (patch)
treee20fa63417ac9981796af9cb5de8a3e1c1e5764b /2024_rust/src/lib.rs
parentff064b6f13019b15346ff320a486d70b95d80b2a (diff)
downloadAoC-822fbc0ecd51b1c7da32b31a0ffc23d09a06c7c6.tar.gz
2024.6
Diffstat (limited to '2024_rust/src/lib.rs')
-rw-r--r--2024_rust/src/lib.rs49
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,