From 18202694cfc4c715de7dab36639373f9deb1abe3 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 25 Dec 2022 00:43:42 -0500 Subject: day 22 part 1 --- src/grid.rs | 112 +++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 73 insertions(+), 39 deletions(-) (limited to 'src/grid.rs') diff --git a/src/grid.rs b/src/grid.rs index d988ec6..040b92b 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -76,6 +76,34 @@ impl std::ops::Sub for usize { } } +impl std::ops::Mul for Row { + type Output = Self; + fn mul(self, other: usize) -> Self::Output { + Self(self.0 * other) + } +} + +impl std::ops::Mul for usize { + type Output = Row; + fn mul(self, other: Row) -> Self::Output { + Row(self * other.0) + } +} + +impl std::ops::Mul for Col { + type Output = Self; + fn mul(self, other: usize) -> Self::Output { + Self(self.0 * other) + } +} + +impl std::ops::Mul for usize { + type Output = Col; + fn mul(self, other: Col) -> Self::Output { + Col(self * other.0) + } +} + impl std::ops::Rem for Row { type Output = Self; fn rem(self, other: usize) -> Self::Output { @@ -183,11 +211,11 @@ impl std::ops::Sub for isize { } #[derive(Default, Clone, Debug, Eq, PartialEq, Hash)] -pub struct GridRow { +pub struct GridRow { cells: Vec, } -impl GridRow { +impl GridRow { pub fn iter(&self) -> impl Iterator + Clone { self.cells.iter() } @@ -197,8 +225,8 @@ impl GridRow { } } -impl - std::ops::Index for GridRow +impl std::ops::Index + for GridRow { type Output = T; fn index(&self, col: Col) -> &Self::Output { @@ -206,29 +234,26 @@ impl } } -impl - std::ops::IndexMut for GridRow +impl std::ops::IndexMut + for GridRow { fn index_mut(&mut self, col: Col) -> &mut Self::Output { &mut self.cells[col.0] } } -#[derive(Default, Clone, Debug, Eq, PartialEq, Hash)] -pub struct Grid { +#[derive(Clone, Debug, Eq, PartialEq, Hash)] +pub struct Grid { rows: Vec>, } -impl Grid { - pub fn grow(&mut self, rows: Row, cols: Col) { - self.rows - .resize_with(rows.0.max(self.rows.len()), GridRow::default); - for row in &mut self.rows { - row.cells - .resize_with(cols.0.max(row.cells.len()), T::default); - } +impl Default for Grid { + fn default() -> Self { + Self { rows: vec![] } } +} +impl Grid { pub fn unshift_rows(&mut self, count: usize) { self.rows = self.rows.split_off(count); } @@ -299,14 +324,19 @@ impl Grid { } } -impl< - T: Default - + Clone - + Eq - + PartialEq - + std::hash::Hash - + std::fmt::Display, - > Grid +impl Grid { + pub fn grow(&mut self, rows: Row, cols: Col) { + self.rows + .resize_with(rows.0.max(self.rows.len()), GridRow::default); + for row in &mut self.rows { + row.cells + .resize_with(cols.0.max(row.cells.len()), T::default); + } + } +} + +impl + Grid { pub fn display_packed char>( &self, @@ -316,14 +346,8 @@ impl< } } -impl< - T: Default - + Clone - + Eq - + PartialEq - + std::hash::Hash - + std::fmt::Display, - > std::fmt::Display for Grid +impl + std::fmt::Display for Grid { fn fmt( &self, @@ -339,8 +363,8 @@ impl< } } -impl - std::ops::Index for Grid +impl std::ops::Index + for Grid { type Output = GridRow; fn index(&self, row: Row) -> &Self::Output { @@ -348,8 +372,8 @@ impl } } -impl - std::ops::IndexMut for Grid +impl std::ops::IndexMut + for Grid { fn index_mut(&mut self, row: Row) -> &mut Self::Output { &mut self.rows[row.0] @@ -363,9 +387,19 @@ impl where I: IntoIterator>, { - Self { + let mut self_ = Self { rows: iter.into_iter().map(|v| GridRow { cells: v }).collect(), - } + }; + let nrows = self_.rows.len(); + let ncols = self_ + .rows + .iter() + .map(|row| row.cells.len()) + .max() + .unwrap_or(0); + self_.grow(Row(nrows), Col(ncols)); + + self_ } } @@ -387,7 +421,7 @@ impl pub struct DisplayPacked< 'a, - T: Default + Clone + Eq + PartialEq + std::hash::Hash + std::fmt::Display, + T: Clone + Eq + PartialEq + std::hash::Hash + std::fmt::Display, F: Fn(&'a T) -> char, >(&'a Grid, F); -- cgit v1.2.3-54-g00ecf