Added day 11

This commit is contained in:
Dennis Brentjes 2023-12-13 21:22:12 +01:00
parent 60c9122fa4
commit 9cdb1c0dcf
4 changed files with 240 additions and 1 deletions

140
resources/input11.txt Normal file
View File

@ -0,0 +1,140 @@
......................................#.....................................#......#.......#.....................#.................#........
............................................................................................................................................
.............................................#............................................................................................#.
..........................#..................................#........#............................#........................................
...#...........................#.................#............................................#...............................#.............
....................#.................................#..................................................#.........#........................
......................................#...............................................................................................#.....
...................................................................................#.............#..........................................
.......................#.........................................#......................#..................................#.....#..........
...............#................#...................#.........................#.................................#...........................
.......#..............................................................#.....................................................................
........................................#...................#.............................................#.................................
...........................#.....................#..................................................#..................#....................
.................#........................................................................#.......................#.........................
............#..................................................#...............................#........................................#...
.#................................#.........................................................................#...............................
.....................................................#...............#....................................................#.................
..........................#...................#....................................#.................#.........................#............
........#..............................#............................................................................#.......................
........................................................#................#..................................................................
...................#.............#...............................#.........................................#................................
........................#...........................#.........................#......#.....................................................#
..#........#.................#....................................................................................................#.........
.............................................#.............#.........................................#............#.........................
...............#......................................................#......................#..........................................#...
............................................................................................................................................
..................................................................................#........................................#................
..........................#........#......#..............#.....#................................................................#...........
.........................................................................................#.....#..............#.............................
....................#..........#.....................#................................................................................#.....
.#....................................................................#........#...........................................................#
..........#..............................................................................................................#..................
............................................................#......................................#........................................
...............#..........#................................................................................#................................
........................................................#..................................#.....................................#..........
......................#................#...............................................................................................#....
........#.......................................#.........................................................................#.................
.........................................................................#........#..................................#......................
#..................................#......#............................................................#...........................#........
............................................................................................................................................
......#.................................................#..............................#....................................................
.........................#....................................................#..............................................#..........#...
.................................#.............................................................................#............................
.................#..................................#................#...........................#..........................................
..............................................................#......................#......#...............................................
.............#..........................#.................................................................................................#.
...............................#........................#...................................................................................
....................................#.................................................................#..............#......................
.......#....................................................................#.................#................................#............
..........................#.................#.....#.................#....................................................#.............#....
............................................................................................................................................
............#.............................................................................................#......#................#.........
...#..........................#..........................#......................................#...........................................
............................................................................................................................................
........#.............#...................................................#.................................................................
...........................................#....................#.................#...........................#......#.........#............
............................................................................................................................................
............#....................#.............................................................#......#.....................................
.....................................................................#..................#...................................................
.....................#..............................#........#...............#........................................................#.....
....................................#...................................................................................#...................
..................................................................................................................#.........................
.....#.......#...............#..................................................................#..............................#............
.............................................#.........#....................................................................................
..................................................................#.......#................................#................................
.........................#.................................................................#.......#.................#......................
#...............................................#.........................................................................#.......#.........
...........................................#.................#................#...........................................................#.
......................#...............#............................................#........................................................
...............#..............#....................#....................................#.........................#.........................
..#..........................................................................................................#..............................
..........#...................................................................................#........#....................................
...........................#.......#.........#.........#..................#.................................................................
................................................................#......................................................#....................
............................................................................................................................................
...............................................................................................................................#............
........#...............................#............#...................................................#........#....................#....
....................#.......................................................................................................................
............#...................................#....................................#................................#.....#...............
.............................#....................................#...............................#.........................................
...#....................................................#..................#...........................#....................................
.......................#..............#..................................................#...............................#........#.........
..............#.................#............................................................................#..............................
........#..........................................#......................................................................................#.
..........................#............................................#.....#.......................................#......................
...................................#.....................#........#...............#.........#...............................................
...........................................................................................................................#................
.........................................#.............................................................#...........................#........
......#.........................#...........................#............#.....#............................................................
...............#.....#.................................#.......................................................................#............
..................................................................................................#..............#........................#.
...................................#.......#................................................................................................
..............................................................#......................#..................#.........................#.........
..............................#.................#.........................................#.................................................
..#.........................................................................................................................................
..................#..................................................#......#......................#........................#...............
...........#...........................................................................................................#..............#.....
.....................................#......................................................................................................
........................................................#.........................................................#.........................
..........................#....................................#............................................................................
............................................................................................#...............#............................#..
#..............................................#...................................#..................#.....................................
.........#.............#..................#.....................................................................#...........................
.............................................................................................................................#..............
.....................................#...........................#............#..................#.....................................#....
.................#..........#............................#..............................................................#........#..........
.........................................................................#...................#..............................................
.................................#...........................................................................#.....#........................
........................................#..........................#........................................................................
...#.....#.................................................#......................................#......#..........................#.....#.
....................................#.............................................#.........................................................
....................#.......#................#..............................#...............................................................
.....................................................................#....................#....................#............................
.................................................#................................................................................#.........
...........#...........#...................................................................................................................#
................#.......................#.........................#...................................................#.....................
...#.......................#...............................#.........................................#.........................#............
....................................................................................#.......................................................
.............................................................................................#...........................................#..
..............#...............................#................................................................#..........#.................
.......................................................#.............#.....#................................................................
............................#...................................................................#...........................................
.....#..............................#...........................................................................................#..........#
..................................................#...............#................#........................................................
..........#..........#.......................................................................................#.........#.............#......
........................................................#....................................................................#..............
.............................#...................................................................................#.......................#..
.#.........................................#...................#...........#..........................#.....................................
....................................................................#......................#................................................
...........#......................................................................#.................................#.......................
.....#.................................................................................#.......................................#............
...............................#.....#..................................................................#...................................
....................#.........................#......................................................................................#......
..................................................................................................#...........#........#....................
...............#.........................................#............#.....................................................................
..................................#......#....................................#.............................................................
...........................#........................#.....................................#..............#...............................#..
....#..............#..............................................................................................#.........................
.............#...................................................................................#..........................................
...................................................................................#..................#..................#............#.....

97
src/days/day11.rs Normal file
View File

@ -0,0 +1,97 @@
use std::{path::Path, str::FromStr, collections::HashSet, ops::Div, cmp::{min, max}};
use itertools::Itertools;
use num::Integer;
use crate::{error::AdventError, input::read_into};
struct Chart {
stride: usize,
space: Vec<char>,
empty_rows: HashSet<usize>,
empty_columns: HashSet<usize>
}
impl FromStr for Chart {
type Err = AdventError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let stride = s.lines().next().ok_or(AdventError("No first line".into()))?.len();
Ok(Chart {
stride,
space: s.chars().filter(|c| !c.is_ascii_whitespace()).collect_vec(),
empty_rows: HashSet::<usize>::new(),
empty_columns: HashSet::<usize>::new(),
})
}
}
impl Chart {
fn detect_doubles(&mut self) {
for x in 0..self.stride {
if self.space.iter().skip(x).step_by(self.stride).all(|c| *c == '.') {
self.empty_columns.insert(x);
}
}
for y in 0..self.space.len().div(self.stride) {
if self.space.iter().skip(y*self.stride).take(self.stride).all(|c| *c == '.') {
self.empty_rows.insert(y);
}
}
}
fn find_galaxys(&self) -> Vec<(usize, usize)> {
let mut vec = vec![];
for (i, c) in self.space.iter().enumerate() {
if *c == '#' {
let x = i.div_mod_floor(&self.stride);
vec.push((x.1, x.0));
}
}
vec
}
fn distance(&self, c1: &(usize, usize), c2: &(usize, usize), extra_distance: u64) -> u64 {
let mut steps = 0;
for x in (min(c1.0, c2.0)..=max(c1.0, c2.0)).into_iter().skip(1) {
steps += 1;
if self.empty_columns.contains(&x) {
steps += extra_distance;
}
}
for y in (min(c1.1, c2.1)..=max(c1.1, c2.1)).into_iter().skip(1) {
steps += 1;
if (self.empty_rows).contains(&y) {
steps += extra_distance;
}
}
steps
}
}
pub fn one() -> Result<u64, AdventError> {
let mut chart = read_into::<Chart>(Path::new("resources/input11.txt"))?;
chart.detect_doubles();
let galaxy_coords = chart.find_galaxys();
let mut sum: u64 = 0;
for (i, c1) in galaxy_coords.iter().enumerate() {
for c2 in galaxy_coords.iter().skip(i+1) {
sum += chart.distance(c1, c2, 2-1);
}
}
Ok(sum as u64)
}
pub fn two() -> Result<u64, AdventError> {
let mut chart = read_into::<Chart>(Path::new("resources/input11.txt"))?;
chart.detect_doubles();
let galaxy_coords = chart.find_galaxys();
let mut sum: u64 = 0;
for (i, c1) in galaxy_coords.iter().enumerate() {
for c2 in galaxy_coords.iter().skip(i+1) {
sum += chart.distance(c1, c2, 1000000-1);
}
}
Ok(sum as u64)
}

View File

@ -7,4 +7,5 @@ pub mod day6;
pub mod day7;
pub mod day8;
pub mod day9;
pub mod day10;
pub mod day10;
pub mod day11;

View File

@ -21,6 +21,7 @@ fn main() -> Result<(), AdventError> {
(day8::one as fn() -> Result<u64, AdventError>, day8::two as fn() -> Result<u64, AdventError>),
(day9::one as fn() -> Result<u64, AdventError>, day9::two as fn() -> Result<u64, AdventError>),
(day10::one as fn() -> Result<u64, AdventError>, day10::two as fn() -> Result<u64, AdventError>),
(day11::one as fn() -> Result<u64, AdventError>, day11::two as fn() -> Result<u64, AdventError>),
];
if env::args().len() == 1 {