From e520118ac294e895bb2dc1afabb6da27b6ab4c59 Mon Sep 17 00:00:00 2001 From: Mathis Date: Fri, 22 Dec 2023 14:40:07 +0100 Subject: [PATCH] 2023-11: Implement part two --- src/year_2023/day_11_cosmic_expantion/mod.rs | 6 +- .../day_11_cosmic_expantion/part_two.rs | 77 +++++++++++++++++++ 2 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 src/year_2023/day_11_cosmic_expantion/part_two.rs diff --git a/src/year_2023/day_11_cosmic_expantion/mod.rs b/src/year_2023/day_11_cosmic_expantion/mod.rs index 742785e..0af860c 100644 --- a/src/year_2023/day_11_cosmic_expantion/mod.rs +++ b/src/year_2023/day_11_cosmic_expantion/mod.rs @@ -1,5 +1,5 @@ pub mod part_one; -//pub mod part_two; +pub mod part_two; pub mod common; use crate::utils::solution::Solution; @@ -19,7 +19,7 @@ impl Solution for DaySolution { } fn part_two(&self) -> String { - //input_reader::read_input_file(self.input_path, part_two::part_two) - String::from("Not implemented") + input_reader::read_input_file(self.input_path(), part_two::part_two) + //String::from("Not implemented") } } \ No newline at end of file diff --git a/src/year_2023/day_11_cosmic_expantion/part_two.rs b/src/year_2023/day_11_cosmic_expantion/part_two.rs new file mode 100644 index 0000000..af9d5c8 --- /dev/null +++ b/src/year_2023/day_11_cosmic_expantion/part_two.rs @@ -0,0 +1,77 @@ +use std::{collections::HashSet, usize}; + +use super::common::{all_false, Galaxy}; + +pub fn part_two(input_lines: Vec) -> String { + let vert_dim = input_lines.len(); + let horz_dim = input_lines[0].len(); + + let mut matrix = vec![vec![false; horz_dim]; vert_dim]; + let mut galaxies: Vec = Vec::new(); + + for (y, line) in input_lines.iter().enumerate() { + for (x, c) in line.chars().enumerate() { + matrix[y][x] = c == '#'; + } + } + + let mut empty_rows_idx: HashSet = HashSet::new(); + for i in 0..matrix.len() { + if all_false(&matrix[i]) { + empty_rows_idx.insert(i); + } + } + + let mut emppty_cols_idx: HashSet = HashSet::new(); + for i in 0..matrix[0].len() { + if all_false(&matrix.iter().map(|row| row[i]).collect::>()) { + emppty_cols_idx.insert(i); + } + } + + for x in 0..matrix.len() { + for y in 0..matrix[x].len() { + if matrix[x][y] { + galaxies.push(Galaxy { i: x, j: y }); + } + } + } + + let mut total_distance = 0; + for i in 0..(galaxies.len() - 1) { + total_distance += distances_from_galaxy(i, &galaxies, &empty_rows_idx, &emppty_cols_idx); + } + + total_distance.to_string() +} + +fn distances_from_galaxy(id : usize, galaxies: &Vec, empty_rows_idx: &HashSet, emppty_cols_idx: &HashSet) -> usize { + let mut distance: usize = 0; + for i in (id + 1)..galaxies.len() { + distance += expanded_euclidian_distance(galaxies[id].i, galaxies[id].j, galaxies[i].i, galaxies[i].j, empty_rows_idx, emppty_cols_idx); + } + distance +} + +fn expanded_euclidian_distance(x1: usize, y1: usize, x2: usize, y2: usize, empty_rows_idx: &HashSet, emppty_cols_idx: &HashSet) -> usize { + expand_distance(x1, x2, empty_rows_idx) + expand_distance(y1, y2, emppty_cols_idx) +} + +fn expand_distance(a: usize, b: usize, empty_ids: &HashSet) -> usize { + + if a > b { + return expand_distance(b, a, empty_ids); + } + + let mut res = 0; + + for i in a..b { + if empty_ids.contains(&i) { + res += 1000000; + } else { + res += 1; + } + } + + res +} \ No newline at end of file