From e3ab089ce3c48d8343b905767f0677f07c4eb885 Mon Sep 17 00:00:00 2001 From: Tyler Hallada Date: Wed, 2 Jan 2019 01:32:15 -0500 Subject: [PATCH] Completed day 6 part 2 --- src/day6.rs | 30 ++++++++++++++++++++++++++++-- src/main.rs | 1 + 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/day6.rs b/src/day6.rs index f1f28b1..ff2f3f1 100644 --- a/src/day6.rs +++ b/src/day6.rs @@ -114,6 +114,13 @@ pub fn solve_part1() -> Result> { Ok(find_largest_coord_area(grid)) } +pub fn solve_part2() -> Result> { + let coords = read_coordinates(INPUT)?; + let boundary_coord = get_boundary_coordinate(&coords); + let grid = create_grid(boundary_coord); + Ok(region_closest_to_coordinates_size(grid, coords)) +} + fn read_coordinates(filename: &str) -> Result, Box> { let mut records: Vec = Vec::new(); lazy_static! { @@ -258,13 +265,32 @@ fn find_largest_coord_area( *count += 1; } }, - GridPoint::Unfilled { x: _, y: _ } | - GridPoint::Tied { x: _, y: _, closest_dist: _ } => {} + _ => () } } *point_count.values().max().unwrap_or(&0) } +fn region_closest_to_coordinates_size(grid: Grid, coords: Vec) -> u32 { + let mut points_in_region = 0; + for point in grid.points.iter() { + match point { + GridPoint::Filled { x, y, closest_coord: _, closest_dist: _ } | + GridPoint::Tied { x, y, closest_dist: _ } | + GridPoint::Unfilled { x, y } => { + let mut sum = 0; + for coord in coords.iter() { + sum += manhattan_dist(coord.x, coord.y, *x, *y); + } + if sum < 10000 { + points_in_region += 1; + } + } + } + } + points_in_region +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/main.rs b/src/main.rs index 2e810fc..7af8cb4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,4 +26,5 @@ fn main() { // println!("{}", day5::solve_part2().unwrap()); println!("Day 6:"); println!("{}", day6::solve_part1().unwrap()); + println!("{}", day6::solve_part2().unwrap()); }