diff options
author | Jesse Luehrs <doy@tozt.net> | 2021-12-20 13:26:14 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2021-12-20 13:26:14 -0500 |
commit | 438e87ea9e746aa8a85c9c6243ea7f111f82292f (patch) | |
tree | cbc9f9e73d37f31fc59daa241657b32ad5f1c5b0 /src/util/grid.rs | |
parent | 46dbae56862f6abf10d48b543a7eb2114dd0c7b4 (diff) | |
download | advent-of-code-438e87ea9e746aa8a85c9c6243ea7f111f82292f.tar.gz advent-of-code-438e87ea9e746aa8a85c9c6243ea7f111f82292f.zip |
day 20
Diffstat (limited to 'src/util/grid.rs')
-rw-r--r-- | src/util/grid.rs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/util/grid.rs b/src/util/grid.rs index 47e10fb..c394008 100644 --- a/src/util/grid.rs +++ b/src/util/grid.rs @@ -3,6 +3,34 @@ pub struct Row(pub usize); #[derive(Copy, Clone, Hash, Eq, PartialEq)] pub struct Col(pub usize); +impl std::ops::Add<usize> for Row { + type Output = Self; + fn add(self, other: usize) -> Self::Output { + Self(self.0 + other) + } +} + +impl std::ops::Add<Row> for usize { + type Output = Row; + fn add(self, other: Row) -> Self::Output { + Row(self + other.0) + } +} + +impl std::ops::Add<usize> for Col { + type Output = Self; + fn add(self, other: usize) -> Self::Output { + Self(self.0 + other) + } +} + +impl std::ops::Add<Col> for usize { + type Output = Col; + fn add(self, other: Col) -> Self::Output { + Col(self + other.0) + } +} + #[derive(Default, Clone)] pub struct GridRow<T: Default + Clone> { cells: Vec<T>, @@ -12,6 +40,10 @@ impl<T: Default + Clone> GridRow<T> { pub fn iter(&self) -> impl Iterator<Item = &T> + Clone { self.cells.iter() } + + pub fn get(&self, col: Col) -> Option<&T> { + self.cells.get(col.0) + } } impl<T: Default + Clone> std::ops::Index<Col> for GridRow<T> { @@ -50,6 +82,10 @@ impl<T: Default + Clone> Grid<T> { Col(self.rows[0].cells.len()) } + pub fn get(&self, row: Row) -> Option<&GridRow<T>> { + self.rows.get(row.0) + } + pub fn cells(&self) -> impl Iterator<Item = &T> { self.rows.iter().flat_map(|row| row.cells.iter()) } |