From 8e90fd7fe5cc893472f8f8bf06f7f89203ffa88f Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 11 Dec 2023 14:57:51 +0100 Subject: [PATCH] =?UTF-8?q?day(11):=20factor=20out=20=E2=80=B9count=5Fwith?= =?UTF-8?q?in=E2=80=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- src/bin/day11.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) 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 })