Completed day 6 part 2

This commit is contained in:
Tyler Hallada 2019-01-02 01:32:15 -05:00
parent bd6c348581
commit e3ab089ce3
2 changed files with 29 additions and 2 deletions

View File

@ -114,6 +114,13 @@ pub fn solve_part1() -> Result<u32, Box<Error>> {
Ok(find_largest_coord_area(grid)) Ok(find_largest_coord_area(grid))
} }
pub fn solve_part2() -> Result<u32, Box<Error>> {
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<Vec<Coordinate>, Box<Error>> { fn read_coordinates(filename: &str) -> Result<Vec<Coordinate>, Box<Error>> {
let mut records: Vec<Coordinate> = Vec::new(); let mut records: Vec<Coordinate> = Vec::new();
lazy_static! { lazy_static! {
@ -258,13 +265,32 @@ fn find_largest_coord_area(
*count += 1; *count += 1;
} }
}, },
GridPoint::Unfilled { x: _, y: _ } | _ => ()
GridPoint::Tied { x: _, y: _, closest_dist: _ } => {}
} }
} }
*point_count.values().max().unwrap_or(&0) *point_count.values().max().unwrap_or(&0)
} }
fn region_closest_to_coordinates_size(grid: Grid, coords: Vec<Coordinate>) -> 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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@ -26,4 +26,5 @@ fn main() {
// println!("{}", day5::solve_part2().unwrap()); // println!("{}", day5::solve_part2().unwrap());
println!("Day 6:"); println!("Day 6:");
println!("{}", day6::solve_part1().unwrap()); println!("{}", day6::solve_part1().unwrap());
println!("{}", day6::solve_part2().unwrap());
} }