Browse Source

Completed day 6 part 2

Tyler Hallada 5 years ago
parent
commit
e3ab089ce3
2 changed files with 29 additions and 2 deletions
  1. 28 2
      src/day6.rs
  2. 1 0
      src/main.rs

+ 28 - 2
src/day6.rs

@@ -114,6 +114,13 @@ pub fn solve_part1() -> Result<u32, Box<Error>> {
114 114
     Ok(find_largest_coord_area(grid))
115 115
 }
116 116
 
117
+pub fn solve_part2() -> Result<u32, Box<Error>> {
118
+    let coords = read_coordinates(INPUT)?;
119
+    let boundary_coord = get_boundary_coordinate(&coords);
120
+    let grid = create_grid(boundary_coord);
121
+    Ok(region_closest_to_coordinates_size(grid, coords))
122
+}
123
+
117 124
 fn read_coordinates(filename: &str) -> Result<Vec<Coordinate>, Box<Error>> {
118 125
     let mut records: Vec<Coordinate> = Vec::new();
119 126
     lazy_static! {
@@ -258,13 +265,32 @@ fn find_largest_coord_area(
258 265
                     *count += 1;
259 266
                 }
260 267
             },
261
-            GridPoint::Unfilled { x: _, y: _ } |
262
-                GridPoint::Tied { x: _, y: _, closest_dist: _ } => {}
268
+            _ => ()
263 269
         }
264 270
     }
265 271
     *point_count.values().max().unwrap_or(&0)
266 272
 }
267 273
 
274
+fn region_closest_to_coordinates_size(grid: Grid, coords: Vec<Coordinate>) -> u32 {
275
+    let mut points_in_region = 0;
276
+    for point in grid.points.iter() {
277
+        match point {
278
+            GridPoint::Filled { x, y, closest_coord: _, closest_dist: _ } |
279
+                GridPoint::Tied { x, y, closest_dist: _ } |
280
+                GridPoint::Unfilled { x, y } => {
281
+                let mut sum = 0;
282
+                for coord in coords.iter() {
283
+                    sum += manhattan_dist(coord.x, coord.y, *x, *y);
284
+                }
285
+                if sum < 10000 {
286
+                    points_in_region += 1;
287
+                }
288
+            }
289
+        }
290
+    }
291
+    points_in_region
292
+}
293
+
268 294
 #[cfg(test)]
269 295
 mod tests {
270 296
     use super::*;

+ 1 - 0
src/main.rs

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