Extract common method Vec2::max
This commit is contained in:
parent
c6c6a234c0
commit
82e0348158
3 changed files with 22 additions and 12 deletions
|
@ -67,10 +67,7 @@ impl Maze {
|
||||||
/// Count the number of tiles inside the polygon described by the pipe loop
|
/// 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).
|
/// using the [winding number algorithm](https://en.wikipedia.org/wiki/Point_in_polygon#Winding_number_algorithm).
|
||||||
fn count_empty_tiles_inside(&self) -> usize {
|
fn count_empty_tiles_inside(&self) -> usize {
|
||||||
let max_dimensions = self.tiles.keys().fold(Vec2::<i64>::default(), |a, b| Vec2 {
|
let max_dimensions = self.tiles.keys().copied().reduce(Vec2::max).unwrap();
|
||||||
x: a.x.max(b.x),
|
|
||||||
y: a.y.max(b.y),
|
|
||||||
});
|
|
||||||
let pipe_loop = self.pipe_loop();
|
let pipe_loop = self.pipe_loop();
|
||||||
|
|
||||||
let mut tiles_inside = 0;
|
let mut tiles_inside = 0;
|
||||||
|
|
|
@ -31,7 +31,7 @@ struct Universe {
|
||||||
impl Universe {
|
impl Universe {
|
||||||
fn expand(&mut self, factor: usize) {
|
fn expand(&mut self, factor: usize) {
|
||||||
let add = factor - 1;
|
let add = factor - 1;
|
||||||
let dimensions = self.dimensions();
|
let dimensions = self.galaxies.iter().copied().reduce(Vec2::max).unwrap();
|
||||||
|
|
||||||
// Add additional columns
|
// Add additional columns
|
||||||
for x in (0..=dimensions.x).rev() {
|
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 {
|
fn sum_of_distances(&self) -> usize {
|
||||||
let pairs = self
|
let pairs = self
|
||||||
.galaxies
|
.galaxies
|
||||||
|
|
20
src/lib.rs
20
src/lib.rs
|
@ -121,6 +121,26 @@ impl<T> Vec2<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Vec2<T>
|
||||||
|
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<T> Add for Vec2<T>
|
impl<T> Add for Vec2<T>
|
||||||
where
|
where
|
||||||
T: Add<Output = T>,
|
T: Add<Output = T>,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue