From 1582b556d8bf7218023dd2c0e05d1743cbfec2f7 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Thu, 6 Dec 2018 16:37:19 +0100 Subject: [PATCH] Refactor range of coordinates to own function. --- 2018/src/day06.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/2018/src/day06.rs b/2018/src/day06.rs index b2b0036..8cda239 100644 --- a/2018/src/day06.rs +++ b/2018/src/day06.rs @@ -68,15 +68,18 @@ impl Day06 { self.ymax = my; } + fn range(&self) -> impl Iterator { + iproduct!(0..=self.xmax, 0..=self.ymax) + .map(|x| Coordinate::from(x)) + } + fn compute_claim_grid(&self) -> Vec> { let mut grid = vec![vec![Claim::None; self.xmax + 1]; self.ymax + 1]; - for coordinate in iproduct!(0..=self.xmax, 0..=self.ymax) { + for coordinate in self.range() { let mut cur_dist = usize::max_value(); let mut cur_best = None; - let coordinate = Coordinate::from(coordinate); - for (i, point) in self.points.iter().enumerate() { let dist = point.manhattan(&coordinate); if dist < cur_dist { @@ -98,8 +101,7 @@ impl Day06 { pub fn part2_with_limit(&mut self, input: &mut Read, limit: usize) -> usize { self.read_points(input); - iproduct!(0..=self.xmax, 0..=self.ymax) - .map(|x| Coordinate::from(x)) + self.range() .map(|x| self.points.iter().map(|y| y.manhattan(&x)).sum::()) .filter(|x| x < &limit) .count()