diff --git a/src/bin/10.rs b/src/bin/10.rs index 8e20f05..0376e4f 100644 --- a/src/bin/10.rs +++ b/src/bin/10.rs @@ -67,10 +67,7 @@ impl Maze { /// Count the number of tiles inside the polygon described by the pipe loop /// using the [winding number algorithm](https://en.wikipedia.org/wiki/Point_in_polygon#Winding_number_algorithm). fn count_empty_tiles_inside(&self) -> usize { - let max_dimensions = self.tiles.keys().fold(Vec2::::default(), |a, b| Vec2 { - x: a.x.max(b.x), - y: a.y.max(b.y), - }); + let max_dimensions = self.tiles.keys().copied().reduce(Vec2::max).unwrap(); let pipe_loop = self.pipe_loop(); let mut tiles_inside = 0; diff --git a/src/bin/11.rs b/src/bin/11.rs index 1e09131..a59a351 100644 --- a/src/bin/11.rs +++ b/src/bin/11.rs @@ -31,7 +31,7 @@ struct Universe { impl Universe { fn expand(&mut self, factor: usize) { let add = factor - 1; - let dimensions = self.dimensions(); + let dimensions = self.galaxies.iter().copied().reduce(Vec2::max).unwrap(); // Add additional columns for x in (0..=dimensions.x).rev() { @@ -60,13 +60,6 @@ impl Universe { } } - fn dimensions(&self) -> Galaxy { - self.galaxies.iter().fold(Galaxy::default(), |a, b| Galaxy { - x: a.x.max(b.x), - y: a.y.max(b.y), - }) - } - fn sum_of_distances(&self) -> usize { let pairs = self .galaxies diff --git a/src/lib.rs b/src/lib.rs index f564f7b..cf8f538 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -121,6 +121,26 @@ impl Vec2 { } } +impl Vec2 +where + T: Ord, +{ + /// Returns the component-wise maximum of this vector and the other. + /// + /// ```rust + /// # use aoc2023::Vec2; + /// let a = Vec2::new(1, 5); + /// let b = Vec2::new(2, -5); + /// assert_eq!(a.max(b), Vec2::new(2, 5)); + /// ``` + pub fn max(self, other: Self) -> Self { + Self { + x: self.x.max(other.x), + y: self.y.max(other.y), + } + } +} + impl Add for Vec2 where T: Add,