From 364d94c2464165fe2b5ead08a91b33d616e72ffe Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 18 Dec 2023 02:17:31 -0500 Subject: day 18 --- src/grid.rs | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) (limited to 'src/grid.rs') diff --git a/src/grid.rs b/src/grid.rs index b006882..63fab03 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -34,6 +34,13 @@ impl std::ops::Add for usize { } } +impl std::ops::Add for Row { + type Output = Row; + fn add(self, other: Row) -> Self::Output { + Row(self.0 + other.0) + } +} + impl std::ops::Add for Col { type Output = Self; fn add(self, other: usize) -> Self::Output { @@ -48,6 +55,13 @@ impl std::ops::Add for usize { } } +impl std::ops::Add for Col { + type Output = Col; + fn add(self, other: Col) -> Self::Output { + Col(self.0 + other.0) + } +} + impl std::ops::Sub for Row { type Output = Self; fn sub(self, other: usize) -> Self::Output { @@ -168,6 +182,13 @@ impl std::ops::Add for isize { } } +impl std::ops::Add for IRow { + type Output = IRow; + fn add(self, other: IRow) -> Self::Output { + IRow(self.0 + other.0) + } +} + impl std::ops::Add for ICol { type Output = Self; fn add(self, other: isize) -> Self::Output { @@ -182,6 +203,13 @@ impl std::ops::Add for isize { } } +impl std::ops::Add for ICol { + type Output = ICol; + fn add(self, other: ICol) -> Self::Output { + ICol(self.0 + other.0) + } +} + impl std::ops::Sub for IRow { type Output = Self; fn sub(self, other: isize) -> Self::Output { @@ -210,6 +238,34 @@ impl std::ops::Sub for isize { } } +impl std::ops::Mul for IRow { + type Output = Self; + fn mul(self, other: isize) -> Self::Output { + Self(self.0 * other) + } +} + +impl std::ops::Mul for isize { + type Output = IRow; + fn mul(self, other: IRow) -> Self::Output { + IRow(self * other.0) + } +} + +impl std::ops::Mul for ICol { + type Output = Self; + fn mul(self, other: isize) -> Self::Output { + Self(self.0 * other) + } +} + +impl std::ops::Mul for isize { + type Output = ICol; + fn mul(self, other: ICol) -> Self::Output { + ICol(self * other.0) + } +} + #[derive(Default, Clone, Debug, Eq, PartialEq, Hash)] pub struct GridRow { cells: Vec, @@ -330,6 +386,24 @@ impl Grid { pos: 0, } } + + pub fn flood_fill( + &mut self, + row: Row, + col: Col, + fill: &T, + diagonal: bool, + ) { + let mut todo = vec![(row, col)]; + while let Some((row, col)) = todo.pop() { + self[row][col] = fill.clone(); + for (row, col) in self.adjacent(row, col, diagonal) { + if self[row][col] != *fill { + todo.push((row, col)); + } + } + } + } } impl Grid { -- cgit v1.2.3-54-g00ecf