diff --git a/day11/rust/.gitignore b/day11/rust/.gitignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/day11/rust/.gitignore @@ -0,0 +1 @@ +target diff --git a/day11/rust/Cargo.lock b/day11/rust/Cargo.lock new file mode 100644 index 0000000..b21cc6a --- /dev/null +++ b/day11/rust/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "rust" +version = "0.1.0" diff --git a/day11/rust/Cargo.toml b/day11/rust/Cargo.toml new file mode 100644 index 0000000..1ec6963 --- /dev/null +++ b/day11/rust/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "rust" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day11/rust/src/main.rs b/day11/rust/src/main.rs new file mode 100644 index 0000000..5a9a333 --- /dev/null +++ b/day11/rust/src/main.rs @@ -0,0 +1,126 @@ +enum Tile { + Empty, + Galaxy, +} + +#[derive(Clone)] +#[derive(Debug)] +struct Position { + x: usize, + y: usize, +} + +fn main() { + let stdin = std::io::stdin(); + + let mut universe = Vec::new(); + + for line in stdin.lines() { + let line = line.unwrap(); + + let row: Vec<_> = line + .chars() + .map(|e| match e { + '.' => Tile::Empty, + '#' => Tile::Galaxy, + _ => unreachable!(), + }) + .collect(); + + universe.push(row); + } + + let mut original_galaxies = Vec::new(); + + for i in 0..universe.len() { + for j in 0..universe[i].len() { + if let Tile::Galaxy = universe[i][j] { + original_galaxies.push(Position { x: j, y: i }) + } + } + } + + + let mut galaxies = original_galaxies.clone(); + + expand_universe(&universe, &mut galaxies, 2); + + let mut part1 = 0; + + for i in 0..galaxies.len() { + for j in i..galaxies.len() { + part1 += galaxies[i].x.abs_diff(galaxies[j].x); + part1 += galaxies[i].y.abs_diff(galaxies[j].y); + } + } + + println!("{}", part1); + + let mut galaxies = original_galaxies.clone(); + + expand_universe(&universe, &mut galaxies, 1_000_000); + + let mut part2 = 0; + + for i in 0..galaxies.len() { + for j in i..galaxies.len() { + part2 += galaxies[i].x.abs_diff(galaxies[j].x); + part2 += galaxies[i].y.abs_diff(galaxies[j].y); + } + } + + println!("{}", part2); +} + +fn expand_universe( + universe: &Vec>, + galaxies: &mut Vec, + expansion_rate: usize, +) -> () { + let mut galaxy_offsets = Vec::new(); + + for _ in 0..galaxies.len() { + galaxy_offsets.push(Position { x: 0, y: 0 }); + } + + for i in 0..universe.len() { + if universe[i].iter().all(|e| match e { + Tile::Empty => true, + _ => false, + }) { + for (j, galaxy) in galaxies.iter().enumerate() { + if galaxy.y > i { + galaxy_offsets[j].y += expansion_rate - 1; + } + } + } + } + + let mut columns = Vec::new(); + + for i in 0..universe[0].len() { + let mut column = Vec::new(); + for j in 0..universe.len() { + column.push(&universe[j][i]); + } + columns.push(column); + } + + for i in 0..columns.len() { + if columns[i].iter().all(|e| match e { + Tile::Empty => true, + _ => false, + }) { + for (j, galaxy) in galaxies.iter().enumerate() { + if galaxy.x > i { + galaxy_offsets[j].x += expansion_rate - 1; + } + } + } + } + + for (i, galaxy) in galaxies.iter_mut().enumerate() { + galaxy.x += galaxy_offsets[i].x; + galaxy.y += galaxy_offsets[i].y; + } +}