From 87d04eb2c3e09e4f01a12eb08461b22bbbcf28d6 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 17 Dec 2022 02:59:05 -0500 Subject: day 17 --- src/grid.rs | 69 ++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 21 deletions(-) (limited to 'src/grid.rs') diff --git a/src/grid.rs b/src/grid.rs index 96ac933..347c902 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -154,12 +154,12 @@ impl std::ops::Sub for isize { } } -#[derive(Default, Clone, Debug, Eq, PartialEq)] -pub struct GridRow { +#[derive(Default, Clone, Debug, Eq, PartialEq, Hash)] +pub struct GridRow { cells: Vec, } -impl GridRow { +impl GridRow { pub fn iter(&self) -> impl Iterator + Clone { self.cells.iter() } @@ -169,8 +169,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 { @@ -178,20 +178,20 @@ impl std::ops::Index } } -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)] -pub struct Grid { +#[derive(Default, Clone, Debug, Eq, PartialEq, Hash)] +pub struct Grid { rows: Vec>, } -impl Grid { +impl Grid { pub fn grow(&mut self, rows: Row, cols: Col) { self.rows .resize_with(rows.0.max(self.rows.len()), GridRow::default); @@ -201,6 +201,10 @@ impl Grid { } } + pub fn unshift_rows(&mut self, count: usize) { + self.rows = self.rows.split_off(count); + } + pub fn rows(&self) -> Row { Row(self.rows.len()) } @@ -267,7 +271,15 @@ impl Grid { } } -impl Grid { +impl< + T: Default + + Clone + + Eq + + PartialEq + + std::hash::Hash + + std::fmt::Display, + > Grid +{ pub fn display_packed char>( &self, f: F, @@ -276,8 +288,14 @@ impl Grid { } } -impl - std::fmt::Display for Grid +impl< + T: Default + + Clone + + Eq + + PartialEq + + std::hash::Hash + + std::fmt::Display, + > std::fmt::Display for Grid { fn fmt( &self, @@ -293,22 +311,26 @@ impl } } -impl std::ops::Index for Grid { +impl + std::ops::Index for Grid +{ type Output = GridRow; fn index(&self, row: Row) -> &Self::Output { &self.rows[row.0] } } -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] } } -impl FromIterator> for Grid { +impl + FromIterator> for Grid +{ fn from_iter(iter: I) -> Self where I: IntoIterator>, @@ -319,8 +341,8 @@ impl FromIterator> for Grid { } } -impl FromIterator<((Row, Col), T)> - for Grid +impl + FromIterator<((Row, Col), T)> for Grid { fn from_iter(iter: I) -> Self where @@ -337,13 +359,18 @@ impl FromIterator<((Row, Col), T)> pub struct DisplayPacked< 'a, - T: Default + Clone + Eq + PartialEq + std::fmt::Display, + T: Default + Clone + Eq + PartialEq + std::hash::Hash + std::fmt::Display, F: Fn(&'a T) -> char, >(&'a Grid, F); impl< 'a, - T: Default + Clone + Eq + PartialEq + std::fmt::Display, + T: Default + + Clone + + Eq + + PartialEq + + std::hash::Hash + + std::fmt::Display, F: Fn(&'a T) -> char, > std::fmt::Display for DisplayPacked<'a, T, F> { -- cgit v1.2.3-54-g00ecf