From 438e87ea9e746aa8a85c9c6243ea7f111f82292f Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 20 Dec 2021 13:26:14 -0500 Subject: day 20 --- src/util/grid.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src/util/grid.rs') 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 for Row { + type Output = Self; + fn add(self, other: usize) -> Self::Output { + Self(self.0 + other) + } +} + +impl std::ops::Add for usize { + type Output = Row; + fn add(self, other: Row) -> Self::Output { + Row(self + other.0) + } +} + +impl std::ops::Add for Col { + type Output = Self; + fn add(self, other: usize) -> Self::Output { + Self(self.0 + other) + } +} + +impl std::ops::Add for usize { + type Output = Col; + fn add(self, other: Col) -> Self::Output { + Col(self + other.0) + } +} + #[derive(Default, Clone)] pub struct GridRow { cells: Vec, @@ -12,6 +40,10 @@ impl GridRow { pub fn iter(&self) -> impl Iterator + Clone { self.cells.iter() } + + pub fn get(&self, col: Col) -> Option<&T> { + self.cells.get(col.0) + } } impl std::ops::Index for GridRow { @@ -50,6 +82,10 @@ impl Grid { Col(self.rows[0].cells.len()) } + pub fn get(&self, row: Row) -> Option<&GridRow> { + self.rows.get(row.0) + } + pub fn cells(&self) -> impl Iterator { self.rows.iter().flat_map(|row| row.cells.iter()) } -- cgit v1.2.3-54-g00ecf