Day11 Rust
This commit is contained in:
1
day11/rust/.gitignore
vendored
Normal file
1
day11/rust/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
target
|
||||
7
day11/rust/Cargo.lock
generated
Normal file
7
day11/rust/Cargo.lock
generated
Normal file
@@ -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"
|
||||
8
day11/rust/Cargo.toml
Normal file
8
day11/rust/Cargo.toml
Normal file
@@ -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]
|
||||
126
day11/rust/src/main.rs
Normal file
126
day11/rust/src/main.rs
Normal file
@@ -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<Vec<Tile>>,
|
||||
galaxies: &mut Vec<Position>,
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user