From dc00f93a03b31747e251d8fbe4d5698342e8fb22 Mon Sep 17 00:00:00 2001 From: Wesley Moore Date: Thu, 13 Dec 2018 07:48:25 +1100 Subject: [PATCH] Add day 6 2018 part 2 --- 2018/src/bin/day6.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/2018/src/bin/day6.rs b/2018/src/bin/day6.rs index 1cb4232..c1068e5 100644 --- a/2018/src/bin/day6.rs +++ b/2018/src/bin/day6.rs @@ -119,6 +119,7 @@ fn main() { .expect("error parsing input"); part1(&coordinates); + part2(&coordinates); } fn part1(coordinates: &[Coordinate]) { @@ -135,6 +136,30 @@ fn part1(coordinates: &[Coordinate]) { println!("Part 1 = {}", area); } +fn part2(coordinates: &[Coordinate]) { + // For each location calculate the distance to each coordinate. If that's less than 10,000 the + // it's part of "the region", count it. + let max = furthest_coord(coordinates); + let mut count_in_region = 0; + + for y in 0..max.y { + for x in 0..max.x { + let current_location = Coordinate { x, y }; + // Sum distance to all the coordinates + let sum = coordinates + .iter() + .map(|coord| current_location.distance_to(coord)) + .sum::(); + + if sum < 10000 { + count_in_region += 1; + } + } + } + + println!("Part 2 = {}", count_in_region); +} + fn furthest_coord(coordinates: &[Coordinate]) -> Coordinate { // There are no negative coords, so assume origin at 0,0 let mut max = Coordinate { x: 0, y: 0 };