From 5616c7212c6f5c40c9a00125a79f850a335f7ee4 Mon Sep 17 00:00:00 2001 From: Mathis Date: Fri, 22 Dec 2023 13:24:51 +0100 Subject: [PATCH] 2023-11: Implement first part --- input/2023/day_11/input.txt | 140 ++++++++++++++++++ .../day_11_cosmic_expantion/common.rs | 15 ++ src/year_2023/day_11_cosmic_expantion/mod.rs | 4 +- .../day_11_cosmic_expantion/part_one.rs | 48 +++++- 4 files changed, 198 insertions(+), 9 deletions(-) create mode 100644 input/2023/day_11/input.txt create mode 100644 src/year_2023/day_11_cosmic_expantion/common.rs diff --git a/input/2023/day_11/input.txt b/input/2023/day_11/input.txt new file mode 100644 index 0000000..7e4e12b --- /dev/null +++ b/input/2023/day_11/input.txt @@ -0,0 +1,140 @@ +..........................#.........................#........................................................#.............................. +.............#.....................................................#.........#......................#....................................... +..................................#....................................................................................#...........#........ +....................#..........................#..................................................................#......................... +.......................................................#...............................................................................#.... +.....................................#.........................#......#..............#...................................................... +........#...............#......................................................#..........#................................#...............# +.........................................#................#..............................................#............#..................... +....#............................#.......................................................................................................... +............#......................................#..............................................#............#............................ +......................#.....................................................................#............................................... +.............................................................................#........#.........................................#........... +.................#.....................................................#.................................................#..............#... +................................#....................#.................................................#.................................... +.......................................#.............................................................................#...................... +..#.....................#................................................................................................................... +............................................#.....................#...........#.....................#....................................... +..................................................................................................................#..................#...... +....................................#....................#................................................................................#. +.........#.....................................#..............#......................#......................................#............... +..............#..........................................................#......................#.....#..................................... +.......................................................................................................................................#.... +..................................................................#.........................#...................#........................... +....#............................#.....#.....................................#................................................#............. +.......................#..........................#......................................................................................... +..................................................................................#.....#...................#.............#................. +................#.............................................#.................................#........................................... +#.....................................................................................................#...................................#. +...........#..............#.....#.............#.....................................................................#....................... +...........................................................................#................#.....................................#......... +...................................................#........................................................................................ +..............#....................................................................................#........................................ +.....................................#................................................#.......................#............................. +.....................................................................................................................................#.....# +........#................#....................................#.................................#.........#................................. +....................................................#..............#............#....................................#...................... +....#...............#........#..........................................................#.......................................#........... +..........................................#.....#.......................#..........................#.....................................#.. +.......................................................#.................................................................................... +#......#...................................................................................................................#................ +............#...............................................................................#..........#..........................#......... +............................................................................................................#............................... +.....................................#....................#........#............................#........................................... +........................#...........................................................#................................#....................#. +......#...............................................#.....................#........................#...................................... +.................................#.....................................#..................................................#................. +...................#......................#..................#..........................#.............................................#..... +.............................#.....................#..............#...........................................#............................. +.........................................................#.......................................................................#.......... +...............................................................................#.......................#.................................#.. +............................................................................................................................................ +............................................................................................................................................ +.................#.............................................................................................#.......#.............#...... +....#.....#....................................................#........#...............#....................................#.............. +..................................#..........#........................................................#..................................... +............................................................................................................................................ +...................#..............................................................................#......................#.................. +............................................................................................................................................ +..#..............................................#......#.....#...........................#..............#....................#............. +................#......#.........#.......................................................................................................... +.........................................................................#......................................#.........................#. +....................................................#..............#............................#........................................... +.....#....................#..........................................................................#...................................... +............#...........................#......#............................................................................................ +............................................................................................................................................ +.....................#.........................................................#.........................#........#.............#........... +.........#..............................................................#.....................#..........................................#.. +......................................#...........#.....................................#................................................... +.#...............................#.......................................................................................................... +..............#............#....................................#..................................#........................#............... +.....................................................................#...........................................#.......................... +............................................................................................................................................ +.........#........#........................#.....#........................#.......................................................#.....#... +............................................................................................................................................ +...................................#........................................................................................................ +..............#..........#..............#....................................#..........#.....#............................................. +....#........................................#............#...................................................................#............. +............................................................................................................................................ +#........#......................................................#........#.................................................................# +....................#.........#..........................................................................#.......#......#................... +............................................................................................................................................ +.......................................#..............#...........................................#......................................... +................................................#.............................................................#.......................#..... +.......................#...................#..............................#................................................................. +...............#...................#....................................................#.............................#..................... +....#......................................................................................................#................................ +.............................#.........................................#....................#.............................#.....#........... +..............................................#..................................................#...................................#...... +...................#...................#..........................................#.................................#....................... +............#.....................#....................#..................#................................................................. +...........................................#........................................................#....................................... +...........................................................................................................#................................ +...#............................................................#.............................................................#............. +.........................#..............................................................#................................................#.. +...............#............................................................................................................................ +.......................................#.......#.............#.............................................................................. +.....................#...........#.............................................#............#.................#.........#...........#....... +..........................................................................#.......................#......................................... +.......#............................................................................#....................................................... +....................................................#....................................................................................#.. +.................#........................................#.....#...........................................................#............... +.#.........#..................................#.................................#..............#.........#.....#......#..................... +.....................#............#.......................................................#.........#....................................... +............................................................................#.......................................................#....... +.....................................................................................#....................................#................. +............................#................................................................#..................................#........... +.....#.........................................................#............................................................................ +......................#..................#............................................................................#..................... +................................................................................#.....................................................#..... +#.................................#.......................................#................................................................. +...................#......#.......................#...............#...........................................#............................. +.............................................................#........................................#......................#.............. +.................................................................................................#.......................................... +..#..................................#....................................................................#......#..................#....... +..........................................................#..................#........................................#..................... +..................#........#........................................................#..........................................#............ +.................................#.............#.......................#...................................................................# +................................................................................................#.........................#................. +....#.....#................................................................................................................................. +.......................#....................................................#............................................................... +....................................................#....................................................................................... +.............#..............................#..........................................#.................................................... +...............................#............................................................................................#........#...... +.#.......................................................#.........#...........#.................................#.......................... +....................................#.............#..............................................................................#.......... +.....................#.........................................................................#......#..................................... +..........................#...........................#.........#.................#......................................................... +..........#.................................#................................#.............................................................. +....#...........................................................................................................#..................#........ +........................................................................#..............#.....................................#.............. +..................#.......................................#................................................................................. +........................................................................................................................................#... +....................................................#.......................#..........................#.................................... +.....#..............................#.........................#..................................#.......................................... +..........................#............................................#........................................#.......#................... +..........................................#................................................................................................. +......................................................................................................................................#..... +.......#...........................................#................................#........................................#.............. +#..................#..........................#..........................#.....#............................#............................... +...................................#.....................................................................................#.................. \ No newline at end of file diff --git a/src/year_2023/day_11_cosmic_expantion/common.rs b/src/year_2023/day_11_cosmic_expantion/common.rs new file mode 100644 index 0000000..0614aaf --- /dev/null +++ b/src/year_2023/day_11_cosmic_expantion/common.rs @@ -0,0 +1,15 @@ +pub struct Galaxy { + pub i: usize, + pub j: usize, +} + +pub fn all_false(vector: &Vec) -> bool { + vector.iter().all(|x| *x == false) +} + +#[allow(dead_code)] +pub fn print_matrix(matrix: &Vec>) { + for row in matrix { + row.iter().for_each(|x| print!("{}", if *x { '#' } else { '.' })); + println!();} +} \ No newline at end of file diff --git a/src/year_2023/day_11_cosmic_expantion/mod.rs b/src/year_2023/day_11_cosmic_expantion/mod.rs index 744b964..742785e 100644 --- a/src/year_2023/day_11_cosmic_expantion/mod.rs +++ b/src/year_2023/day_11_cosmic_expantion/mod.rs @@ -1,6 +1,6 @@ pub mod part_one; //pub mod part_two; -//pub mod common; +pub mod common; use crate::utils::solution::Solution; use crate::utils::input_reader; @@ -11,7 +11,7 @@ pub struct DaySolution ; impl Solution for DaySolution { fn input_path(&self) -> &'static str { - "input/2023/day_11/input_test.txt" + "input/2023/day_11/input.txt" } fn part_one(&self) -> String { diff --git a/src/year_2023/day_11_cosmic_expantion/part_one.rs b/src/year_2023/day_11_cosmic_expantion/part_one.rs index 4a535c7..19f4ff0 100644 --- a/src/year_2023/day_11_cosmic_expantion/part_one.rs +++ b/src/year_2023/day_11_cosmic_expantion/part_one.rs @@ -1,23 +1,35 @@ +use super::common::{all_false, Galaxy}; + pub fn part_one(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[x][y] = c == '#'; + matrix[y][x] = c == '#'; } } - println!("Pre-expand dims: {:?}, {:?}", matrix.len(), matrix[0].len()); - expand_rows(&mut matrix); expand_cols(&mut matrix); - println!("Post-expand dims: {:?}, {:?}", matrix.len(), matrix[0].len()); + 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); + } - String::from("Not implemented") + total_distance.to_string() } fn expand_rows(matrix: &mut Vec>) { @@ -46,6 +58,28 @@ fn expand_cols(matrix: &mut Vec>) { } } -fn all_false(vector: &Vec) -> bool { - vector.iter().all(|x| *x == false) +fn distances_from_galaxy(id : usize, galaxies: &Vec) -> usize { + let mut distance: usize = 0; + for i in (id + 1)..galaxies.len() { + distance += euclidian_distance(galaxies[id].i, galaxies[id].j, galaxies[i].i, galaxies[i].j); + } + distance +} + +fn euclidian_distance(x1: usize, y1: usize, x2: usize, y2: usize) -> usize { + let x; + if x1 > x2 { + x = x1 - x2; + } else { + x = x2 - x1; + } + + let y; + if y1 > y2 { + y = y1 - y2; + } else { + y = y2 - y1; + } + + x + y } \ No newline at end of file