1
0
Fork 0

day(11): factor out ‹count_within›

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2023-12-11 14:57:51 +01:00
parent 63d495b175
commit 8e90fd7fe5
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -17,6 +17,10 @@ struct Day11 {
}
impl Day11 {
fn count_within(xs: &HashSet<usize>, min_x: usize, max_x: usize) -> usize {
xs.iter().filter(|&&x| min_x < x && x < max_x).count()
}
fn solve(&self, factor: usize) -> usize {
let to_add = factor - 1;
@ -31,16 +35,8 @@ impl Day11 {
let mut xd = x0.abs_diff(x1);
let mut yd = y0.abs_diff(y1);
let min_x = min(x0, x1);
let max_x = max(x0, x1);
xd += to_add
* self
.empty_xs
.iter()
.filter(|&&x| min_x < x && x < max_x)
.count();
yd += to_add * self.empty_ys.iter().filter(|&&y| y0 < y && y < y1).count();
xd += to_add * Self::count_within(&self.empty_xs, min(x0, x1), max(x0, x1));
yd += to_add * Self::count_within(&self.empty_ys, y0, y1);
xd + yd
})