diff --git a/src/bin/day11.rs b/src/bin/day11.rs index 1bdb771..d87880c 100644 --- a/src/bin/day11.rs +++ b/src/bin/day11.rs @@ -17,6 +17,10 @@ struct Day11 { } impl Day11 { + fn count_within(xs: &HashSet, 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 })