From 6a23bbad3b1dbadaa41e2172dc4257915156cf18 Mon Sep 17 00:00:00 2001 From: Anders Olsson Date: Mon, 17 Dec 2018 14:02:49 +0100 Subject: [PATCH] 2018/06 --- 2018/06/Cargo.lock | 4 ++ 2018/06/example-01.txt | 6 +++ 2018/06/input.txt | 50 ++++++++++++++++++ 2018/06/src/main.rs | 116 ++++++++++++++++++++++++++++++++++++++++- 4 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 2018/06/Cargo.lock create mode 100644 2018/06/example-01.txt create mode 100644 2018/06/input.txt diff --git a/2018/06/Cargo.lock b/2018/06/Cargo.lock new file mode 100644 index 0000000..c87e1a0 --- /dev/null +++ b/2018/06/Cargo.lock @@ -0,0 +1,4 @@ +[[package]] +name = "day-06" +version = "0.1.0" + diff --git a/2018/06/example-01.txt b/2018/06/example-01.txt new file mode 100644 index 0000000..95d160a --- /dev/null +++ b/2018/06/example-01.txt @@ -0,0 +1,6 @@ +1, 1 +1, 6 +8, 3 +3, 4 +5, 5 +8, 9 diff --git a/2018/06/input.txt b/2018/06/input.txt new file mode 100644 index 0000000..7cf5491 --- /dev/null +++ b/2018/06/input.txt @@ -0,0 +1,50 @@ +194, 200 +299, 244 +269, 329 +292, 55 +211, 63 +123, 311 +212, 90 +292, 169 +359, 177 +354, 95 +101, 47 +95, 79 +95, 287 +294, 126 +81, 267 +330, 78 +202, 165 +225, 178 +266, 272 +351, 326 +180, 62 +102, 178 +151, 101 +343, 145 +205, 312 +74, 193 +221, 56 +89, 89 +242, 172 +59, 138 +83, 179 +223, 88 +297, 234 +147, 351 +226, 320 +358, 338 +321, 172 +54, 122 +263, 165 +126, 341 +64, 132 +264, 306 +72, 202 +98, 49 +238, 67 +310, 303 +277, 281 +222, 318 +357, 169 +123, 225 diff --git a/2018/06/src/main.rs b/2018/06/src/main.rs index e7a11a9..2466f17 100644 --- a/2018/06/src/main.rs +++ b/2018/06/src/main.rs @@ -1,3 +1,115 @@ -fn main() { - println!("Hello, world!"); +use std::collections::HashMap; +use std::io::{self, BufRead}; + +fn distance(a: (i32, i32), b: (i32, i32)) -> i32 { + (a.0 - b.0).abs() + (a.1 - b.1).abs() +} + +#[derive(Clone, Copy, Debug)] +enum State { + Empty, + Owner(usize, i32), + Equal(i32), +} + +#[derive(Clone, Copy, Debug)] +enum Area { + Infinite, + Finite(usize), +} + +fn main() { + let coordinates = io::stdin() + .lock() + .lines() + .filter_map(Result::ok) + .map(|line| { + let coords = line + .split(", ") + .map(|coord| coord.parse::().unwrap()) + .collect::>(); + + (coords[0], coords[1]) + }) + .collect::>(); + + let width = coordinates + .iter() + .map(|(x, _)| *x as usize) + .max().expect("failed to find max x") + 1; + + let height = coordinates + .iter() + .map(|(_, y)| *y as usize) + .max() + .expect("failed to find max y") + 1; + + let mut grid_view = vec![State::Empty; width * height].into_boxed_slice(); + + for x in 0..width { + for y in 0..height { + for (i, c) in coordinates.iter().enumerate() { + let distance = distance(*c, (x as i32, y as i32)); + let state = &mut grid_view[x + (y * width)]; + + *state = match state { + State::Empty => State::Owner(i, distance), + State::Owner(_, d) if *d > distance => State::Owner(i, distance), + State::Owner(o, d) if *d == distance && *o != i => State::Equal(distance), + State::Equal(d) if *d > distance => State::Owner(i, distance), + _ => *state, + }; + } + } + } + + let mut areas = HashMap::new(); + + for x in 0..width { + for y in 0..height { + if let State::Owner(o, _) = grid_view[x + (y * width)] { + let a = areas.entry(o).or_insert(Area::Finite(0)); + + if x == 0 || x == width - 1 || y == 0 || y == height - 1 { + *a = Area::Infinite; + } else { + match a { + Area::Infinite => (), + Area::Finite(area) => *area += 1, + } + } + } + } + } + + let (_, area) = areas + .iter() + .filter_map(|(i, area)| if let Area::Finite(area) = area { + Some((i, area)) + } else { + None + }) + .max_by_key(|(_, area)| *area) + .expect("failed to find max area"); + + println!("part.one={}", area); + + let mut grid_view = vec!['.'; width * height].into_boxed_slice(); + + for y in 0..height { + for x in 0..width { + let total = coordinates + .iter() + .map(|c| distance(*c, (x as i32, y as i32))) + .sum::(); + + if total < 10_000 { + grid_view[x + (y * width)] = '#'; + } + } + } + + let area = grid_view.iter().filter(|r| **r == '#').count(); + + println!("part.two={}", area); }