From 5ab8409b5b65ec54baaffc883b2dd4fadeacb925 Mon Sep 17 00:00:00 2001 From: Johannes Schaefer Date: Thu, 6 Dec 2018 09:50:35 +0100 Subject: [PATCH] day06 both --- input/day06.txt | 50 +++++++++++++++ src/main.rs | 4 +- src/tasks/day06.rs | 150 +++++++++++++++++++++++++++++++++++++++++++++ src/tasks/mod.rs | 1 + 4 files changed, 203 insertions(+), 2 deletions(-) create mode 100644 input/day06.txt create mode 100644 src/tasks/day06.rs diff --git a/input/day06.txt b/input/day06.txt new file mode 100644 index 0000000..285ccb3 --- /dev/null +++ b/input/day06.txt @@ -0,0 +1,50 @@ +152, 292 +163, 90 +258, 65 +123, 147 +342, 42 +325, 185 +69, 45 +249, 336 +92, 134 +230, 241 +74, 262 +241, 78 +299, 58 +231, 146 +239, 87 +44, 157 +156, 340 +227, 226 +212, 318 +194, 135 +235, 146 +171, 197 +160, 59 +218, 205 +323, 102 +290, 356 +244, 214 +174, 250 +70, 331 +288, 80 +268, 128 +359, 98 +78, 249 +221, 48 +321, 228 +52, 225 +151, 302 +183, 150 +142, 327 +172, 56 +72, 321 +225, 298 +265, 300 +86, 288 +78, 120 +146, 345 +268, 181 +243, 235 +262, 268 +40, 60 diff --git a/src/main.rs b/src/main.rs index cf60aed..06fcc20 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ fn main() { - aoc_2018::tasks::day05::task1(); - aoc_2018::tasks::day05::task2(); + // aoc_2018::tasks::day06::task1(); + aoc_2018::tasks::day06::task2(); } diff --git a/src/tasks/day06.rs b/src/tasks/day06.rs new file mode 100644 index 0000000..28f7c14 --- /dev/null +++ b/src/tasks/day06.rs @@ -0,0 +1,150 @@ +use utils; + +pub fn task1() { + use self::Bla::*; + let input = utils::read_file("input/day06.txt"); + + let coordinates: Vec<(u16, u16)> = input + .lines() + .map(|line| { + let mut split = line.split(", "); + ( + split.next().unwrap().parse().unwrap(), + split.next().unwrap().parse().unwrap(), + ) + }).collect(); + + let max_x = coordinates.iter().max_by_key(|it| it.0).unwrap().0; + let max_y = coordinates.iter().max_by_key(|it| it.1).unwrap().1; + + let mut area: Vec> = Vec::new(); + for _ in 0..max_x { + let mut vec = Vec::new(); + for _ in 0..max_y { + vec.push(Bla::None); + } + area.push(vec); + } + + for (_, (a, b)) in coordinates.iter().enumerate() { + for x in 0..area.len() { + for y in 0..area[x].len() { + let d = (i32::abs(*a as i32 - x as i32) + i32::abs(*b as i32 - y as i32)) as u16; + area[x][y] = match area[x][y] { + None => Single(d, (*a, *b)), + Single(dd, (aa, bb)) => { + if dd < d { + Single(dd, (aa, bb)) + } else if dd > d { + Single(d, (*a, *b)) + } else { + // equal + Multi(d) + } + } + Multi(dd) => { + if d < dd { + Single(d, (*a, *b)) + } else { + Multi(dd) + } + } + } + } + } + } + + let occupation = coordinates + .iter() + .enumerate() + .map(|(_, (a, b))| { + let count = area + .iter() + .flat_map(|v| v) + .filter(|entry| { + if let Single(_, (x, y)) = entry { + if a == x && b == y { + true + } else { + false + } + } else { + false + } + }).count(); + let infinite = area[0].iter().any(|bla| bla.belongs_to_point(*a, *b)) + || area[area.len() - 1] + .iter() + .any(|bla| bla.belongs_to_point(*a, *b)) + || area.iter().any(|line| line[0].belongs_to_point(*a, *b)) + || area + .iter() + .any(|line| line[line.len() - 1].belongs_to_point(*a, *b)); + // println!("{} has size {} (infinite: {:?})", i, count, infinite); + (count, infinite) + }).collect::>(); + + println!( + "Overall occupation: {} of {}", + occupation.iter().map(|(count, _)| *count).sum::(), + area.len() * area[0].len() + ); + + let result = occupation + .iter() + .filter(|(_, infinite)| !infinite) + .max_by_key(|x| x.0); + + println!("{:?}", result); // 5224 too high +} + +#[derive(Debug)] +enum Bla { + None, + /// distance, node + Single(u16, (u16, u16)), + /// distance + Multi(u16), +} + +impl Bla { + fn belongs_to_point(&self, x: u16, y: u16) -> bool { + match self { + Bla::Single(_, (a, b)) => *a == x && *b == y, + _ => false, + } + } +} + +pub fn task2() { + let input = utils::read_file("input/day06.txt"); + + let coordinates: Vec<(u16, u16)> = input + .lines() + .map(|line| { + let mut split = line.split(", "); + ( + split.next().unwrap().parse().unwrap(), + split.next().unwrap().parse().unwrap(), + ) + }).collect(); + + let max_x = coordinates.iter().max_by_key(|it| it.0).unwrap().0; + let max_y = coordinates.iter().max_by_key(|it| it.1).unwrap().1; + + let result: usize = (0..max_x) + .map(|x| { + (0..max_y) + .map(|y| { + coordinates + .iter() + .map(|(a, b)| { + (i32::abs(*a as i32 - x as i32) + i32::abs(*b as i32 - y as i32)) + as usize + }).sum::() + }).filter(|it| *it < 10000) + .count() + }).sum(); + + println!("Part 2: {}", result); +} diff --git a/src/tasks/mod.rs b/src/tasks/mod.rs index f290640..d945a75 100644 --- a/src/tasks/mod.rs +++ b/src/tasks/mod.rs @@ -3,3 +3,4 @@ pub mod day02; pub mod day03; pub mod day04; pub mod day05; +pub mod day06;