Day 11 Complete

This commit is contained in:
Luke Else 2023-12-11 16:54:27 +00:00
parent 5c46e12269
commit 4082df6be8
5 changed files with 228 additions and 9 deletions

View File

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

View File

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

View File

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

View File

@ -18,7 +18,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
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 {}),

View File

@ -1,20 +1,30 @@
use super::Solution;
use itertools::Itertools;
pub struct Day11 {}
impl Solution for Day11 {
fn part1(
&self,
_input: &mut Vec<String>,
input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> {
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<String>,
input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> {
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<String>) -> (Vec<&'a [u8]>, Vec<(usize, usize)>) {
let universes = input.iter().map(|r| r.as_bytes()).collect::<Vec<_>>();
// 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::<Vec<_>>();
(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");
}
}