Refactor range of coordinates to own function.

This commit is contained in:
2018-12-06 16:37:19 +01:00
parent c44ebaa238
commit 1582b556d8

View File

@@ -68,15 +68,18 @@ impl Day06 {
self.ymax = my; self.ymax = my;
} }
fn range(&self) -> impl Iterator<Item=Coordinate> {
iproduct!(0..=self.xmax, 0..=self.ymax)
.map(|x| Coordinate::from(x))
}
fn compute_claim_grid(&self) -> Vec<Vec<Claim>> { fn compute_claim_grid(&self) -> Vec<Vec<Claim>> {
let mut grid = vec![vec![Claim::None; self.xmax + 1]; self.ymax + 1]; 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_dist = usize::max_value();
let mut cur_best = None; let mut cur_best = None;
let coordinate = Coordinate::from(coordinate);
for (i, point) in self.points.iter().enumerate() { for (i, point) in self.points.iter().enumerate() {
let dist = point.manhattan(&coordinate); let dist = point.manhattan(&coordinate);
if dist < cur_dist { if dist < cur_dist {
@@ -98,8 +101,7 @@ impl Day06 {
pub fn part2_with_limit(&mut self, input: &mut Read, limit: usize) -> usize { pub fn part2_with_limit(&mut self, input: &mut Read, limit: usize) -> usize {
self.read_points(input); self.read_points(input);
iproduct!(0..=self.xmax, 0..=self.ymax) self.range()
.map(|x| Coordinate::from(x))
.map(|x| self.points.iter().map(|y| y.manhattan(&x)).sum::<usize>()) .map(|x| self.points.iter().map(|y| y.manhattan(&x)).sum::<usize>())
.filter(|x| x < &limit) .filter(|x| x < &limit)
.count() .count()