mirror of
https://github.com/wezm/advent-of-code.git
synced 2024-11-14 03:28:01 +00:00
Add day 6 2018 part 2
This commit is contained in:
parent
7b04f00291
commit
dc00f93a03
1 changed files with 25 additions and 0 deletions
|
@ -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::<u32>();
|
||||
|
||||
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 };
|
||||
|
|
Loading…
Reference in a new issue