diff --git a/day11/Cargo.lock b/day11/Cargo.lock new file mode 100644 index 0000000..2cd0acd --- /dev/null +++ b/day11/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "anyhow" +version = "1.0.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9d19de80eff169429ac1e9f48fffb163916b448a44e8e046186232046d9e1f9" + +[[package]] +name = "day11" +version = "0.1.0" +dependencies = [ + "anyhow", +] diff --git a/day11/Cargo.toml b/day11/Cargo.toml new file mode 100644 index 0000000..60e5b31 --- /dev/null +++ b/day11/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day11" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0.77" diff --git a/day11/input b/day11/input new file mode 100644 index 0000000..7c322e1 --- /dev/null +++ b/day11/input @@ -0,0 +1,140 @@ +.............................................................................................#.............................................. +...#....................#............................#......................#........................#...........#................#......... +................#.....................................................#..............#.....................#................................ +................................................................#.......................................................#................... +.......#........................#.........................#..............................#.....#............................................ +............#............................#.................................................................................................. +......................#........................#......................................................#..................................... +....................................#.....................................#.................#..........................................#.... +................................................................................................................#..............#............ +...................#.................................................#............................#......................................... +............................................#.........#...............................#.................#................................... +.....#.....................#..................................................#............................................................. +........................................................................#.................#................................................. +...............................................................#.....................................................#............#......... +..............#.......................#...........................................#......................................................... +.......................#.......................#...........................................................#................................ +.........#........................................................#......................................................................... +.........................................................................#................................................................#. +............................#..........................#........................#.....#...............................#..................... +.....................................#..................................................................#....................#.............. +#........................................................................................................................................... +..........#......................#...........#............#............#.................................................................... +....................#.........................................................#.................................#........................... +.................................................#.............#.....................#..........#......................#..........#......... +....................................#....................................................................................................... +..........................................#..............................................................................................#.. +......................#.....................................................................................#.....#.........#............... +...........#................#.............................#.............#.....................#............................................. +#.................#...............................................#...........#............................................................. +.......................................#.................................................#.................................................. +.....#.......................................#.......................................................#..............................#......# +..............#.................#.................#..................#..............#..............................#........................ +...........................................................................#................................................................ +.........................#............................#....................................................#................#............... +.#..............................................................................#........................................................... +.......................................#................................#......................#............................................ +................#...........#...................#.......................................................................................#... +..............................................................#..........................................................#.................. +.......#......................................................................#.........#.............#..................................... +.......................................................#.................................................................................... +......................#................................................#....................................#.............................#. +#.........................................#......................................................................#.......................... +..................................#................#............................................#.......#................................... +.....#.....................#.............................#.............................#.................................................... +................#...............................................#........................................................................... +...............................#................................................#....................#..........................#........... +......................................................#....................#................#................#.............................. +..#..........................................#......................................................................#....................... +.............#.............................................................................................................................. +.......................................................................................................................................#.... +.......................#..............#...........................#............................#...............#............................ +.........#..................#...............................................#...........#................................................... +......................................................................#........................................................#............ +..............................................................#....................#...............#......#........#........................ +..............#...................#.............#.....................................................................................#..... +............................................................................................................................................ +.....................#.....#............#...........#......................................#................................................ +..........#...................................................................................................................#...........#. +...#......................................................................#.....................#................#.......................... +.............................................#...........................................................................#.................. +......................................................#...........#................#................#.................................#..... +.................................#.....................................#..................#...............#......................#.......... +............................................................................................................................................ +.......#..............................................................................................................#..................... +.....................#..............#......#.................................................#.............................................. +..............................#..................................#..............#............................#.............................. +..........#...............................................#............................#..........................................#......... +#.................................................#...................#...........................#......................................... +.......................................#.................................................................................................... +.......#...........................................................................#........................................................ +................#............................................#............................................#.........#....................#.. +.......................#........#.........#...............................................................................#................. +............................................................................................................................................ +..#................#........................................................................................................................ +..........#...........................................#......................................#.............................................. +............................................................................................................................................ +.....#.....................................#...................#....................................................................#....... +.........................#...........#............#.................................................................#....................#.. +........................................................#...............#................................................................... +............#.................#.................................................#......#..............#..................................... +.............................................................................................#................#................#............ +........................................#.......................#..........................................................................# +..#.................#..........................#......................#.............#..............#...................#.............#...... +............................#............................#................................#......................#.......................... +.............#.................................................................................#............................................ +.................................#.........................................#................................................................ +.........................#.........................#.................................................#........................#............. +.......#......................................................#............................................................................. +............................................................................................................................................ +............................................................................................................................................ +.......................#.........................................................................................#.......................... +...#...........................#..................................................................#......#................#................. +...............................................#................#........................................................................... +....................#.................................#.................#.......#..........#........................................#....... +...........#.................................................................................................#.............................# +.#..................................................................................................#....................................... +.....................................#.................................................................................#.................... +...............#.........#...........................................................#...................................................... +................................#........................#........#......................................................................... +......#..........................................#.............................................................................#............ +...............................................................................................#.......#.................................... +...............................................................................................................#............................ +..............................#...........#............................#.................................................#............#..... +...................#...........................#............................................................................................ +...............................................................#.....................#.....#................................................ +........#.................#.......................................................................................#...........#............. +...............#............................#.........#.......................#...........................#................................. +.#...................................#...................................#................................................................#. +.........................................................................................#...........................................#...... +........................#................................................................................................................... +....................................................................................................................#....................... +........................................#.....#.........#.........#......................................................................... +.......................................................................................#............#....................................... +.#..............................#.............................................................#........................#.....#.....#........ +............#...............................................................#.................................#.........................#... +..............................................................#.........................................#................................... +........................#..............................#...............#........#.........#................................................. +...................#........................#.......................................................................#....................... +.......................................#...................................................................#................................ +..............#..................#..............#...................................................................................#....... +........#......................................................#..............#.................#.........................................#. +....................................................................#.......................................................#............... +.............................#..........................................................................#................................... +.................#..................................................................#....................................................... +............................................#..........................#............................................#..............#........ +......#..............#..........................................#...........#..............................#................................ +............#.....................#.................................................................#....................................... +...........................#.............#...............#..................................................................#............... +#....................................................................................................................................#...... +.............................................................#.....................#........................................................ +.......#..........................................#.................#....................................#........#.......................#. +..........................................................................................#................................................. +..........................#..............................................#......................#........................................... +...............................#.....................#..........................#.............................#............................. +....#......#................................................#..........................#.......................................#............ +..................#.............................#........................................................................................... +...........................................#..................................................#............................................. +.......................#...........................................#...................................................#...................# +.......#......#.............#.........................#..................#..............................#...........................#....... +..#..............................................................................................................#.......................... diff --git a/day11/src/main.rs b/day11/src/main.rs new file mode 100644 index 0000000..0901772 --- /dev/null +++ b/day11/src/main.rs @@ -0,0 +1,71 @@ +use std::io::stdin; + +use anyhow::Result; + +#[derive(Debug)] +struct Space { + galaxies: Vec<(usize, usize)>, +} + +fn main() -> Result<()> { + let space = Space::load()?; + let total: usize = space.pairs() + .map(|(a,b)| manhattan_distance(a,b)) + .sum(); + println!("Part 1: total distance {total}"); + Ok(()) +} + +fn manhattan_distance(a: (usize,usize), b: (usize,usize)) -> usize { + a.0.abs_diff(b.0) + a.1.abs_diff(b.1) +} + +const EXPANSION_FACTOR: usize = 1_000_000 - 1; + +impl Space { + + fn load() -> Result { + let mut galaxies = vec![]; + + let mut empty_lines = vec![]; + let mut non_empty_columns = vec![]; + + for (x, l) in + stdin().lines().enumerate() + { + let mut empty = true; + for (y,c) in l?.chars().enumerate() { + if c == '#' { + empty = false; + galaxies.push((x,y)); + if non_empty_columns.len() <= y { + non_empty_columns.resize(y+1, false); + } + non_empty_columns[y] = true; + } + } + if empty { empty_lines.push(x) }; + } + + let empty_columns: Vec<_> = non_empty_columns.iter() + .enumerate() + .filter_map(|(y, full)| Some(y).filter(|_| !full)) + .collect(); + + for (x,y) in &mut galaxies { + *x += EXPANSION_FACTOR * empty_lines.iter().filter(|&&l| l < *x).count(); + *y += EXPANSION_FACTOR * empty_columns.iter().filter(|&&c| c < *y).count(); + } + + Ok(Space { galaxies }) + } + + fn pairs(&self) -> impl Iterator + '_ { + self.galaxies.iter() + .enumerate() + .flat_map(|(i, &a)| { + self.galaxies.iter().skip(i+1).map(move |&b| { (a,b)}) + }) + } + +} diff --git a/day11/test b/day11/test new file mode 100644 index 0000000..986aad4 --- /dev/null +++ b/day11/test @@ -0,0 +1,10 @@ +...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#.....