Add day 6 2018 part 2

This commit is contained in:
Wesley Moore 2018-12-13 07:48:25 +11:00
parent 7b04f00291
commit dc00f93a03
No known key found for this signature in database
GPG key ID: BF67766C0BC2D0EE

View file

@ -119,6 +119,7 @@ fn main() {
.expect("error parsing input"); .expect("error parsing input");
part1(&coordinates); part1(&coordinates);
part2(&coordinates);
} }
fn part1(coordinates: &[Coordinate]) { fn part1(coordinates: &[Coordinate]) {
@ -135,6 +136,30 @@ fn part1(coordinates: &[Coordinate]) {
println!("Part 1 = {}", area); 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::<u32>();
if sum < 10000 {
count_in_region += 1;
}
}
}
println!("Part 2 = {}", count_in_region);
}
fn furthest_coord(coordinates: &[Coordinate]) -> Coordinate { fn furthest_coord(coordinates: &[Coordinate]) -> Coordinate {
// There are no negative coords, so assume origin at 0,0 // There are no negative coords, so assume origin at 0,0
let mut max = Coordinate { x: 0, y: 0 }; let mut max = Coordinate { x: 0, y: 0 };