From 4082df6be8e1d7437041446a4e676ec1d612bb63 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Mon, 11 Dec 2023 16:54:27 +0000 Subject: [PATCH] Day 11 Complete --- src/input/day11 | 140 +++++++++++++++++++++++++++++++++++++++++ src/input/day11_test1 | 10 +++ src/input/day11_test2 | 10 +++ src/main.rs | 2 +- src/solutions/day11.rs | 75 +++++++++++++++++++--- 5 files changed, 228 insertions(+), 9 deletions(-) diff --git a/src/input/day11 b/src/input/day11 index e69de29..ad933d8 100644 --- a/src/input/day11 +++ b/src/input/day11 @@ -0,0 +1,140 @@ +...................#.....#...............#......................#......................#...................#...........................#.... +..................................................#................................................#..............#......................... +......#.....#.................#..............................................................#.............................................. +......................................................#..................#.....#............................................................ +.....................................................................................#.....................................#................ +.#............................................................................................................#............................. +..................#......................#............................#..............................................................#.....# +......................................................................................................#.............#....................... +..............................................#........#........................#................................................#.......... +.....#......................#......................................#.....#...................#.............................................. +.........................................................................................................#.................................. +......................#...............#............#........................................................................................ +..................................................................................................#......................................... +..........#..................................................................................................#..............#..........#.... +....#...........................#..........................#..............................#................................................. +........................................#............................................#.................................#.........#.......... +...................................................................#...........#..........................#................................. +............................................................................................................................................ +............................................#............................................................................................... +....................#.................................#................................................................................#.... +............................................................................................#............................................... +............................................................................#........#.........................................#............ +...#...................#..........................#...............#......................................................#.................. +..............................#........#.................................................................................................... +................#................................................................................#.......................................#.. +......#..............................................................#...................#.................................................. +.#.........................................#..........#.........#..............#........................#............#...................... +............................................................................................................................................ +...........#...................................#...............................................................#.............#.............. +........................#........#........................................#..........#..................................................#... +....#.....................................................#.................................#......................................#........ +...............................................................#...................................#....................#................... +#.............................................................................#..................................#.............#............ +.............................#..........#.............................#...................................#................................. +........................................................#................................................................................... +.............#........................................................................................................#................#.... +...#.............................................#..................................#......#.....#.......................................... +................................#..............................#............................................................................ +...................#........................................................#............................................................... +#..........................#.........................................................................#......#...........#................... +.......#...........................#.....#..............#.................................................................................#. +......................#.........................#........................................................................................... +.....................................................................................................................#.......#......#....... +.............................................................#....................#...............#......................................... +.........#.................................................................................................................................. +...............................#............#....................#.....#...............#..................#................................. +....#...........#.........#.........................#..............................................................#..................#..... +.....................#.......................................................................#.............................................. +....................................................................................................#....................................... +.....................................#..........#...............................#...........................#..........#..........#.......#. +..........................................................................................#................................................. +.#..........................................#.............................#................................................................. +.................#...........#............................#.............................................#......................#............ +.........#...........................................................#..............#....................................................... +..................................................................................................#.............#.......#................... +.............................................................................#.......................................................#...... +......#.............#...........#..........#........#...........#...........................................#............................... +.........................................................................#.................................................................. +........................................................#...........#...........#................................................#.......... +........................................#....................#..........................................#.............#..................... +..........#...................#................................................................#..........................................#. +..................#.................................................................................#..........#............................ +..........................................................................................#................................................. +..........................................................#...............#................................................................. +............#................................#......................#..........#.........................................#......#........... +..#.......................#..................................................................................#.....#........................ +.................................................#...........................................#.............................................. +.........#........#..................#...........................#.....................#...............#.............................#...... +......................................................................#..........................#.....................#.................... +............................................................................................................................................ +........................#.....................................................................................#............#............#... +..............#............................#.................................#......#....................................................... +........#..........#........................................................................................................................ +............................#............................................#..........................#....................................... +.......................................................#......#.........................................................#......#............ +.........................................................................................#.....#..................#......................... +.......................#................#.......................................#............................#.......................#...... +............................................................................................................................................ +.........#.......................................................................................................................#.......... +...#..........#................................#...........#......#.....................................................................#... +....................................................................................#....................................................... +.............................#.....................................................................................#.....#.................. +...................................#.....................................................#.............#.................................... +.....#.............#................................#......................................................................................# +.............#............................................#...................#.................#.....................#.....#......#........ +#...............................#.........#..........................................#...................................................... +.........................#...............................................#.................................................................. +.................................................................................#......................................................#... +....#...........#..................................#......................................................#................................. +..............................................#.....................#.......................#...........................#................... +..............................#.........................#.............................................#.........#........................... +.........................................#.....................................#...............................................#............ +.#.........#..................................................#..........................#...........................#...................... +............................................................................................................................................ +.........................................................................................................................................#.. +.......#..........................................#.......................#................................................#........#....... +............................................................................................................................................ +.................#.........#.........................................#.........................#.....#......#............................... +............................................#..........#......................#......#...................................................... +....................................#.............................................................................#......................... +...........................................................#................................................................................ +.................................................................................#........#...............#..............................#.. +.......#...........#..............................#......................................................................................... +..#...............................................................................................#.................#............#.......... +..............................................#..........#...............#.................................................................. +..................................#.....#.....................#.......................#...............#......#.............#................ +..........................#................................................................................................................. +....................................................#..................................................................#.................... +...........................................................................#..............#.....#........................................... +#...................#...........................................................#........................................................... +...........................................#....................#....................................................................#...... +.......#.....#........................................#....................................................#................................ +..............................#...................................................................#......................................... +.............................................................................#.......................................#...................... +.....................#....................................................................................................................#. +.................................#....................................#........................#............................................ +......#...........................................#......................................................................................... +...........................................#................................................................................#......#........ +..........#...............#.........................................................#....................................................... +..................#...........................................................#............................................................. +...............................................................#............................................#.......#....................... +....#........#...................................................................................#.......................................... +......................#...................................................#...........#................#.................................... +........#..............................................#...................................#....................#........................... +..................................................#.......................................................................#..........#...... +.#...................................#..........................................................................................#........... +..........................................#........................................#...............#........................................ +..........................#.....#........................................................................................................#.. +..................#...........................#..........#...........................................................#...................... +...................................................#......................#...................................................#............. +.........#................................................................................#................................................. +..............#................................................................#............................................................ +...........................#.........#..............................#...................................#..........#....................#... +..................................................................................................#.............................#........... +..#..............................................#.......................................................................................... +.................#.....................................................#.................#.................................................. +.................................#.........#...................................................#......#.........#........................... +..........#.........................................#.....#......................#.......................................................... +....................#..........................#................#...........................................................#......#........ +#........................#.............#..................................#.................................#............................... \ No newline at end of file diff --git a/src/input/day11_test1 b/src/input/day11_test1 index e69de29..a0bda53 100644 --- a/src/input/day11_test1 +++ b/src/input/day11_test1 @@ -0,0 +1,10 @@ +...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#..... \ No newline at end of file diff --git a/src/input/day11_test2 b/src/input/day11_test2 index e69de29..a0bda53 100644 --- a/src/input/day11_test2 +++ b/src/input/day11_test2 @@ -0,0 +1,10 @@ +...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#..... \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 8e154d2..841162c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ async fn main() -> Result<(), Box> { Box::new(day08::Day08 {}), Box::new(day09::Day09 {}), Box::new(day10::Day10 {}), - // Box::new(day11::Day11 {}), + Box::new(day11::Day11 {}), // Box::new(day12::Day12 {}), // Box::new(day13::Day13 {}), // Box::new(day14::Day14 {}), diff --git a/src/solutions/day11.rs b/src/solutions/day11.rs index ba2a1f4..5d68e20 100644 --- a/src/solutions/day11.rs +++ b/src/solutions/day11.rs @@ -1,20 +1,30 @@ use super::Solution; +use itertools::Itertools; + pub struct Day11 {} impl Solution for Day11 { fn part1( &self, - _input: &mut Vec, + input: &mut Vec, ) -> Result, Box> { - Ok(Box::new("Ready")) + let (universe, mut galaxy) = self.get_universe(input); + + Ok(Box::new(self.get_distance(&universe, &mut galaxy, 2))) } fn part2( &self, - _input: &mut Vec, + input: &mut Vec, ) -> Result, Box> { - Ok(Box::new("Ready")) + let (universe, mut galaxy) = self.get_universe(input); + + Ok(Box::new(self.get_distance( + &universe, + &mut galaxy, + 1_000_000, + ))) } fn get_day(&self) -> u8 { @@ -22,7 +32,56 @@ impl Solution for Day11 { } } -impl Day11 {} +impl Day11 { + fn get_universe<'a>(&'a self, input: &'a Vec) -> (Vec<&'a [u8]>, Vec<(usize, usize)>) { + let universes = input.iter().map(|r| r.as_bytes()).collect::>(); + + // Use the cartesian product between X and Y, (Y, X), to plot coords of galaxies + let galaxies = (0..universes.len()) + .cartesian_product(0..universes[0].len()) + .filter(|(y, x)| universes[*y][*x] == b'#') + .collect::>(); + + (universes, galaxies) + } + + fn get_distance( + &self, + universe: &Vec<&[u8]>, + galaxies: &mut Vec<(usize, usize)>, + expansion: usize, + ) -> usize { + let (rows, cols) = (universe.len(), universe[0].len()); + + let empty_rows = (0..rows).filter(|row| universe[*row].iter().all(|p| *p == b'.')); + let empty_cols = (0..cols).filter(|col| (0..rows).all(|row| universe[row][*col] == b'.')); + + for row in empty_rows.rev() { + for galaxy in galaxies.iter_mut() { + // Increase distance between galaxies by 1 for each empty row they cross + if galaxy.0 > row { + galaxy.0 += expansion - 1; + } + } + } + + for col in empty_cols.rev() { + for galaxy in galaxies.iter_mut() { + // Increase distance between galaxies by 1 for each empty column they cross + if galaxy.1 > col { + galaxy.1 += expansion - 1; + } + } + } + + // get a vector of x, y distances between each galaxy + galaxies + .iter() + .tuple_combinations() + .map(|(g1, g2)| g1.0.abs_diff(g2.0) + g1.1.abs_diff(g2.1)) + .sum() + } +} /// Test from puzzle input #[cfg(test)] @@ -44,7 +103,7 @@ mod test { .unwrap() .to_string(); - assert_eq!(answer, "Ready"); + assert_eq!(answer, "374"); } #[test] @@ -61,6 +120,6 @@ mod test { .unwrap() .to_string(); - assert_eq!(answer, "Ready"); + assert_eq!(answer, "82000210"); } -} \ No newline at end of file +}