From 8a40f62cf6dbcab04748848a6e4729460ec23cb1 Mon Sep 17 00:00:00 2001 From: Andrew Glaze Date: Wed, 13 Dec 2023 15:40:57 -0500 Subject: [PATCH] day11 part1 --- day11/Cargo.lock | 7 ++++++ day11/Cargo.toml | 8 +++++++ day11/input.txt | 10 +++++++++ day11/src/main.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 day11/Cargo.lock create mode 100644 day11/Cargo.toml create mode 100644 day11/input.txt create mode 100644 day11/src/main.rs diff --git a/day11/Cargo.lock b/day11/Cargo.lock new file mode 100644 index 0000000..4ab9be8 --- /dev/null +++ b/day11/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day11" +version = "0.1.0" diff --git a/day11/Cargo.toml b/day11/Cargo.toml new file mode 100644 index 0000000..8f5b9a5 --- /dev/null +++ b/day11/Cargo.toml @@ -0,0 +1,8 @@ +[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] diff --git a/day11/input.txt b/day11/input.txt new file mode 100644 index 0000000..a0bda53 --- /dev/null +++ b/day11/input.txt @@ -0,0 +1,10 @@ +...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#..... \ No newline at end of file diff --git a/day11/src/main.rs b/day11/src/main.rs new file mode 100644 index 0000000..17fb63d --- /dev/null +++ b/day11/src/main.rs @@ -0,0 +1,55 @@ +use std::fs; + +fn main() { + let input = fs::read_to_string("input.txt").unwrap(); + let input: Vec<_> = input.split('\n') + .map(|x| x.chars().collect::>()) + .collect(); + + let mut rotate: Vec> = vec![]; + for j in 0..input[0].len() { + let mut tmp: Vec = vec![]; + for i in 0..input.len() { + tmp.push(input[i][j]); + } + + if tmp.iter().all(|x| x == &'.') { + rotate.push(tmp.clone()); + } + rotate.push(tmp); + } + + + let mut expanded: Vec> = vec![]; + for j in 0..rotate[0].len() { + let mut tmp: Vec = vec![]; + for i in 0..rotate.len() { + tmp.push(rotate[i][j]); + } + if tmp.iter().all(|x| x == &'.') { + expanded.push(tmp.clone()); + } + expanded.push(tmp); + } + + let mut galaxies: Vec<(i32, i32)> = vec![]; + + for (i, line) in expanded.iter().enumerate() { + for (j, char) in line.iter().enumerate() { + if char == &'#' { + galaxies.push((i as i32,j as i32)); + } + } + } + + let dist_total = galaxies.clone().into_iter().enumerate() + .fold(0, |mut acc, (i, gal)| { + for next in &galaxies[i + 1..] { + acc += gal.0.abs_diff(next.0) + gal.1.abs_diff(next.1); + } + acc + }); + + println!("{:?}", dist_total); + +} \ No newline at end of file