From 9918141a472170a8562ca2d5d6d1c3b06a2e8a00 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Thu, 6 Dec 2018 07:32:35 +0200 Subject: Day 6: Manhattan distances and coordinates --- inputs/6.txt | 50 ++++++++++++++++++++++++++++++++ src/bin/day_6.rs | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 inputs/6.txt diff --git a/inputs/6.txt b/inputs/6.txt new file mode 100644 index 0000000..e544bbd --- /dev/null +++ b/inputs/6.txt @@ -0,0 +1,50 @@ +195, 221 +132, 132 +333, 192 +75, 354 +162, 227 +150, 108 +46, 40 +209, 92 +153, 341 +83, 128 +256, 295 +311, 114 +310, 237 +99, 240 +180, 337 +332, 176 +212, 183 +84, 61 +275, 341 +155, 89 +169, 208 +105, 78 +151, 318 +92, 74 +146, 303 +184, 224 +285, 348 +138, 163 +216, 61 +277, 270 +130, 155 +297, 102 +197, 217 +72, 276 +299, 89 +357, 234 +136, 342 +346, 221 +110, 188 +82, 183 +271, 210 +46, 198 +240, 286 +128, 95 +111, 309 +108, 54 +258, 305 +241, 157 +117, 162 +96, 301 diff --git a/src/bin/day_6.rs b/src/bin/day_6.rs index 008ea56..16c8782 100644 --- a/src/bin/day_6.rs +++ b/src/bin/day_6.rs @@ -4,15 +4,100 @@ use advent_of_code_2018::*; use std::error::Error; use std::path::PathBuf; +use std::collections::{HashSet, HashMap}; + // cargo watch -cs "cargo run --release --bin day_6" fn main() -> Result<(), Box> { let input = read_file(&PathBuf::from("inputs/6.txt"))?; + let coordinates = input.iter().map(|line| { + let mut split = line.split(|c: char| !c.is_numeric()); + let x: i32 = split.next().unwrap().parse().unwrap(); + let _ = split.next(); + let y: i32 = split.next().unwrap().parse().unwrap(); + (x, y) + }).collect::>(); + debug!(coordinates); + + let min_x = coordinates.iter().map(|(x, _)| x).min().unwrap().clone(); + let max_x = coordinates.iter().map(|(x, _)| x).max().unwrap().clone(); + let min_y = coordinates.iter().map(|(_, y)| y).min().unwrap().clone(); + let max_y = coordinates.iter().map(|(_, y)| y).max().unwrap().clone(); + + debug!(min_x); + debug!(max_x); + debug!(min_y); + debug!(max_y); + + let mut infinite = HashSet::new(); + for x in min_x..max_x+1 { + for y in &[0, max_y] { + if let Some(coord) = find_closest(&coordinates, x, *y) { + infinite.insert(coord); + } + } + } + for y in min_y..max_y+1 { + for x in &[0, max_x] { + if let Some(coord) = find_closest(&coordinates, *x, y) { + infinite.insert(coord); + } + } + } - println!("Input: {:?}", input); + debug!(infinite); + + let mut areas = HashMap::new(); + for x in min_x..max_x+1 { + for y in min_y..max_y+1 { + if let Some(coord) = find_closest(&coordinates, x, y) { + *areas.entry(coord).or_insert(0) += 1; + } + } + } + debug!(areas); + let biggest_non_infinite = areas.iter() + .filter(|(k,_)| !infinite.contains(&k)) + .max_by_key(|(_,v)| *v); + debug!(biggest_non_infinite); + + + let safe_distance = 10000; + let mut safe_size = 0; + for x in min_x..max_x+1 { + for y in min_y..max_y+1 { + let distance_sum: i32 = coordinates.iter() + .map(|(x1, y1)| manhattan_distance(*x1, *y1, x, y)) + .sum(); + if distance_sum < safe_distance { + safe_size += 1; + } + } + } + debug!(safe_size); + Ok(()) } + +fn find_closest(coordinates: &Vec<(i32, i32)>, x: i32, y: i32) -> Option { + let mut distances = coordinates.iter() + .map(|(x1, y1)| manhattan_distance(*x1, *y1, x, y)) + .enumerate() + .collect::>(); + distances.sort_by_key(|(_, d)| *d); + + if distances[0] == distances[1] { + None + } + else { + distances.first().map(|(i, _)| *i) + } +} + +fn manhattan_distance(x1: i32, y1: i32, x2: i32, y2: i32) -> i32 { + (x2-x1).abs() + (y2-y1).abs() +} -- cgit v1.2.3