From 276ac892402272c2281d407525395a55a2d95f1a Mon Sep 17 00:00:00 2001 From: Mads Jensen Date: Sat, 20 Jul 2024 22:12:16 +0200 Subject: [PATCH 1/3] solution 1 done --- Cargo.lock | 5 + bin/Cargo.toml | 3 +- bin/src/main.rs | 10 ++ inputs/day11.txt | 140 +++++++++++++++++++++++++ lib/observatory/Cargo.toml | 8 ++ lib/observatory/src/lib.rs | 204 +++++++++++++++++++++++++++++++++++++ 6 files changed, 369 insertions(+), 1 deletion(-) create mode 100644 inputs/day11.txt create mode 100644 lib/observatory/Cargo.toml create mode 100644 lib/observatory/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index d407306..2a45cf3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,6 +13,7 @@ dependencies = [ "gondola_lift", "network_nodes", "oasis", + "observatory", "pipe_maze", "scratchcard", "trebuchet", @@ -46,6 +47,10 @@ version = "0.1.0" name = "oasis" version = "0.1.0" +[[package]] +name = "observatory" +version = "0.1.0" + [[package]] name = "pipe_maze" version = "0.1.0" diff --git a/bin/Cargo.toml b/bin/Cargo.toml index 1a3a6ac..576bed1 100644 --- a/bin/Cargo.toml +++ b/bin/Cargo.toml @@ -15,4 +15,5 @@ boat_race = { path = "../lib/boat_race" } camel_cards = { path = "../lib/camel_cards" } network_nodes = { path = "../lib/network_nodes" } oasis = { path = "../lib/oasis" } -pipe_maze = { path = "../lib/pipe_maze" } \ No newline at end of file +pipe_maze = { path = "../lib/pipe_maze" } +observatory = { path = "../lib/observatory" } \ No newline at end of file diff --git a/bin/src/main.rs b/bin/src/main.rs index bede917..c35605d 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs @@ -14,6 +14,7 @@ use cube_game::{cube::Color, Cubes, Game}; use gondola_lift::EngineSchematic; use network_nodes::{Network, Node}; use oasis::Report; +use observatory::Image; use pipe_maze::Maze; use scratchcard::ScratchCards; use trebuchet::Trebuchet; @@ -127,6 +128,14 @@ fn day10(input: String) { println!("Area of nest: {area}"); } +fn day11(input: String) { + let mut image = Image::from_str(&input).expect("Failed to parse image"); + image.expand(); + + let paths_sum = image.find_shortest_paths_sum(); + println!("Shortest paths sum: {paths_sum}"); +} + fn main() { println!("## Advent of Code 2023 solutions ##"); time!("All", { @@ -140,5 +149,6 @@ fn main() { day!(8, day8); day!(9, day9); day!(10, day10); + day!(11, day11); }) } diff --git a/inputs/day11.txt b/inputs/day11.txt new file mode 100644 index 0000000..ca13309 --- /dev/null +++ b/inputs/day11.txt @@ -0,0 +1,140 @@ +................................................#.........#............................................#.................................... +..#.............#......#.............................................#...........................................................#.......... +..............................................................#..............................#.............................................. +.................................................................................................................#.......................... +...........#...........................#...............................................#..................................#.............#... +............................#..................#.....#..........................#........................................................... +..................................#................................#........................................................................ +...........................................................................#................................................................ +.....#...................................#......................................................#.......................#...........#.....#. +.....................#........#....................#....................................................#................................... +......................................................................#..................................................................... +...........................................................#.................................#.............................................. +........#..................#....................#....................................................#...................................... +#.............#....................................................#..............#.............................#........................... +...................#.............#...................................................................................#...............#...... +.............................................................................#.............................................................# +.........................................................................................................#.................................. +....#...........................................................................................#...........................#.....#......... +.....................#.......................#.........#.................................................................................... +#............#.........................................................................#....................#............................... +................................#..................#...........#......#............................#...................#.......#............ +.......................................................................................................................................#.... +...................................................................................#........................................................ +.........................#.............#...................................................................................#................ +............................................................................................................................................ +..........#..............................................................#......#.........#..........#...................................... +............................................................................................................................................ +.................................................................................................#............................#............. +................#...............................#............#.....................#........................................................ +.#........................#..........................#......................................................#.......................#....... +.......#....................................#................................#.............................................................. +.................................#.....................................#..................................................................#. +........................................................#.............................#...............................#........#............ +.....................#...................#.....#...............#............................................................................ +.............#...................................................................................................#.......................... +................................................................................................#.....#...............................#..... +.................#................#................#........................................................................................ +.#..........................#.............................#.......#.......#................................................................# +.............................................#.............................................................#................................ +...........#.................................................................................#...............................#.............. +.....................#.................................#.......#..................................................................#.....#... +.......#..............................#..............................................................#...................................... +#...............................#.....................................#...............#........................#.......#.................... +...........................................................................#.....#.........................................................# +...........................................................................................#................................................ +........................................................#................................................#.......................#.......... +.........#............#............................................#.................................................#.....#................ +............................................................................................................................................ +......................................#......#........................................................#.........#........................... +.............#.....#......#..........................#...................................................................................... +......#................................................................................................................................#.... +.............................................................................................................#.............................. +..#.......#.............................#.....................#..................#........#................................................. +............................................................................................................................................ +......................................................................................#...............................#..................... +..................#...........................#...........................#.........................#.........................#............. +............................................................................................................................................ +......................................#..................................................#.........................................#........ +.......................#.....#................................................................#.............#............#.................. +.....#.............................................#.............#.......................................................................... +.............#...........................................................#.......#..................................#...........#........... +.................................#.......................................................................................................... +.............................................#...........#.................................................................................. +.......#......................................................#............................#.........#........#............................. +.....................#................................................#...........................................................#......... +...............#...................#...............#.......................................................................#................ +#..........................#......................................................................#......................................... +.............................................................................#.............................................................. +...............................#......#.................#..............................................#..........#......................... +...........................................................................................................................................# +.................#......#.......................#....................................#.....#..............................#................. +.........#.............................................................#..................................#................................. +....................................................#............#............#.........................................................#... +..#.........................................................#........................................................#..........#........... +............................................................................................................................................ +............................................................................................................................................ +.......#...........................#..............................................................#......................................... +....................#...................................#................................................................#.................. +..............................#............................................................................................................. +..#......................#.............................................#........#..........#................................................ +..........#.............................#......................#................................................................#........... +.....................................................................................................#...................................... +......................#.............................................................................................#......................# +#................#..................................#........................................#.................#............................ +...................................#......#.......................#...........#..........................................#.............#.... +............................................................................................................................................ +..........#..............................................#.................................................................................. +............................................................................................................................................ +....................#......#..........#.......................#......#....................#..........#.......#..................#........... +...#.........................................................................#.............................................................. +..................................................................................#..............#.......................................... +........#.......................#..........#................................................................................................ +..............#...........................................................................................#................................. +................................................................#.......................................................................#... +.......................#................#.....................................................................#............................. +....#............#............#.................................................................#.....#..................................... +...................................................................................................................#........................ +...........................................#..............................................................................#................. +.......#.........................................#.......#.......#.....#...............................................................#.... +................................................................................#...........#.....................................#......... +...........#.....................................................................................#.......................................... +..............................................................#............................................................................. +...............#.........#..........#........................................................................#.............................. +...........................................#......................................................................#......#.................. +....#...............#....................................................................................................................... +......................................................................................#..............................................#...... +...............................................#...............#..............#.................#........................................... +.#.........................................................................................#.........................#...................... +................#......#................#................................#..........................#...........#........................... +......#......................#.....................#.......#................................................................................ +.............................................#...........................................................#...............#.....#........#... +...........#.........................................................................#...................................................... +............................................................................#............................................................... +.........................................................................................#............................#..................... +...................#...................#.....................#....................................#.............#........................... +.................................#.......................................................................................................... +.......#......#...........#..................................................................................................#.....#........ +.......................................................................#..............#........#.............#.............................. +......................#..................#......#..............#..............#.....................................#..................#.... +..........................................................................................#.............#................................... +.........#..........................................#..............#.......................................................#................ +.............................#.............................................................................................................. +.............................................#...................................#.......................................................... +................#..................#........................................#......................................#........................ +....#.................#.................#.............................................#.................................#..................# +............................................................#.........#..................................#.................................. +......................................................#...................................#................................................. +.................................#..................................................................#....................................... +.................................................#...................................................................................#...... +............................................................................................................................................ +..................#.............................................#.............#.........................#................#.................. +.........#.................................................#...........#.............#........#..............#............................#. +...................................#.........................................................................................#.............. +............................................................................................................................................ +#.....................#...................................................#.......#...............................#......................... +..............................#.................#........#..........................................................................#....... +..............#........................................................................#.................................................... +...........................................#.............................................................#..............................#... +.................................#...................................................................................#...........#.......... +...........#.......#.......#...........................#.....#................#......................#........#............................. \ No newline at end of file diff --git a/lib/observatory/Cargo.toml b/lib/observatory/Cargo.toml new file mode 100644 index 0000000..e8ca582 --- /dev/null +++ b/lib/observatory/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "observatory" +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/lib/observatory/src/lib.rs b/lib/observatory/src/lib.rs new file mode 100644 index 0000000..ad08a86 --- /dev/null +++ b/lib/observatory/src/lib.rs @@ -0,0 +1,204 @@ +use std::{fmt::Display, str::FromStr}; + +#[derive(Debug)] +pub enum ObservatoryError { + ParsePixel(char), +} + +#[derive(Debug)] +pub struct Coordinate { + row: usize, + col: usize, +} + +impl Coordinate { + pub const fn new(row: usize, col: usize) -> Self { + Self { row, col } + } + + pub const fn shortest_path_to(&self, other: &Self) -> usize { + self.col.abs_diff(other.col) + self.row.abs_diff(other.row) + } +} + +#[derive(Debug, PartialEq, Eq)] +pub enum Pixel { + Empty, + Galaxy, +} + +impl TryFrom for Pixel { + type Error = ObservatoryError; + + fn try_from(value: char) -> Result { + let pixel = match value { + '.' => Self::Empty, + '#' => Self::Galaxy, + invalid => return Err(ObservatoryError::ParsePixel(invalid)), + }; + + Ok(pixel) + } +} + +impl Display for Pixel { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let pixel = match self { + Self::Empty => '.', + Self::Galaxy => '#', + }; + write!(f, "{pixel}") + } +} + +pub struct Image { + data: Vec>, +} + +impl FromStr for Image { + type Err = ObservatoryError; + + fn from_str(s: &str) -> Result { + let data = s + .lines() + .map(|row| row.chars().map(Pixel::try_from).collect::, _>>()) + .collect::>, _>>()?; + + Ok(Self { data }) + } +} + +impl Image { + pub fn height(&self) -> usize { + self.data.len() + } + + pub fn width(&self) -> usize { + self.data[0].len() + } + + pub fn expand(&mut self) { + let mut row = 0; + while row < self.height() { + if !self.row_contains_galaxy(row) { + self.add_empty_row(row); + row += 2; + } else { + row += 1; + } + } + + let mut col = 0; + while col < self.width() { + if !self.column_contains_galaxy(col) { + self.add_empty_column(col); + col += 2; + } else { + col += 1; + } + } + } + + fn add_empty_row(&mut self, at_row: usize) { + self.data + .insert(at_row, (0..self.width()).map(|_| Pixel::Empty).collect()); + } + + fn add_empty_column(&mut self, at_col: usize) { + self.data.iter_mut().for_each(|row| row.insert(at_col, Pixel::Empty)); + } + + fn row_contains_galaxy(&self, row: usize) -> bool { + self.data[row].contains(&Pixel::Galaxy) + } + + fn column_contains_galaxy(&self, col: usize) -> bool { + (0..self.height()).any(|row| { + if let Some(pixel) = self.data[row].get(col) { + return pixel == &Pixel::Galaxy; + } + false + }) + } + + fn find_galaxies(&self) -> Vec { + self.data + .iter() + .enumerate() + .flat_map(|(row, row_pixels)| { + row_pixels + .iter() + .enumerate() + .filter_map(|(col, pixel)| { + if pixel == &Pixel::Galaxy { + Some(Coordinate::new(row, col)) + } else { + None + } + }) + .collect::>() + }) + .collect() + } + + pub fn find_shortest_paths_sum(&self) -> usize { + let galaxies = self.find_galaxies(); + + galaxies + .iter() + .enumerate() + .map(|(index, coord)| { + let mut sum = 0; + (index..galaxies.len()).for_each(|other| sum += coord.shortest_path_to(&galaxies[other])); + sum + }) + .sum() + } +} + +impl Display for Image { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let image = self + .data + .iter() + .map(|row| row.iter().map(|col| col.to_string()).collect()) + .collect::>() + .join("\n"); + + write!(f, "{image}") + } +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE: &str = "...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#....."; + + #[test] + fn test_expansion() { + let mut image = Image::from_str(EXAMPLE).expect("Failed to parse image"); + + image.expand(); + + assert_eq!((image.height(), image.width()), (12, 13)); + } + + #[test] + fn solution_1() { + let mut image = Image::from_str(EXAMPLE).expect("Failed to parse image"); + + image.expand(); + + assert_eq!(374, image.find_shortest_paths_sum()); + } +} From cda53cdff7feaae3bf7294ba70a716a1d48ecd98 Mon Sep 17 00:00:00 2001 From: Mads Jensen Date: Fri, 25 Oct 2024 00:11:03 +0200 Subject: [PATCH 2/3] solved --- bin/src/main.rs | 12 ++++- lib/observatory/src/lib.rs | 102 ++++++++++++++++++++++++++----------- 2 files changed, 82 insertions(+), 32 deletions(-) diff --git a/bin/src/main.rs b/bin/src/main.rs index c35605d..0c6210d 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs @@ -129,11 +129,19 @@ fn day10(input: String) { } fn day11(input: String) { - let mut image = Image::from_str(&input).expect("Failed to parse image"); - image.expand(); + let image = Image::from_str(&input).expect("Failed to parse image"); + + let mut image_1 = image.clone(); + image_1.expand(1); let paths_sum = image.find_shortest_paths_sum(); println!("Shortest paths sum: {paths_sum}"); + + let mut image_2 = image; + image_2.expand(1_000_000); + + let paths_sum = image_2.find_shortest_paths_sum(); + println!("Shortest paths sum 1mil: {paths_sum}"); } fn main() { diff --git a/lib/observatory/src/lib.rs b/lib/observatory/src/lib.rs index ad08a86..a7c8456 100644 --- a/lib/observatory/src/lib.rs +++ b/lib/observatory/src/lib.rs @@ -19,20 +19,43 @@ impl Coordinate { pub const fn shortest_path_to(&self, other: &Self) -> usize { self.col.abs_diff(other.col) + self.row.abs_diff(other.row) } + + pub const fn add_row(&self, row: usize) -> Self { + Self { + row: self.row + row, + col: self.col, + } + } + + pub const fn add_col(&self, col: usize) -> Self { + Self { + row: self.row, + col: self.col + col, + } + } } -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum Pixel { - Empty, + Empty(usize), Galaxy, } +impl Pixel { + pub const fn get_length(&self) -> usize { + match self { + Self::Empty(n) => *n, + Self::Galaxy => 1, + } + } +} + impl TryFrom for Pixel { type Error = ObservatoryError; fn try_from(value: char) -> Result { let pixel = match value { - '.' => Self::Empty, + '.' => Self::Empty(1), '#' => Self::Galaxy, invalid => return Err(ObservatoryError::ParsePixel(invalid)), }; @@ -44,13 +67,14 @@ impl TryFrom for Pixel { impl Display for Pixel { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let pixel = match self { - Self::Empty => '.', - Self::Galaxy => '#', + Self::Empty(n) => format!("_.x{}_", n), + Self::Galaxy => "#".to_string(), }; write!(f, "{pixel}") } } +#[derive(Clone)] pub struct Image { data: Vec>, } @@ -77,37 +101,33 @@ impl Image { self.data[0].len() } - pub fn expand(&mut self) { + pub fn expand(&mut self, age: usize) { let mut row = 0; while row < self.height() { if !self.row_contains_galaxy(row) { - self.add_empty_row(row); - row += 2; - } else { - row += 1; + self.data + .get_mut(row) + .expect("Failed to get row") + .iter_mut() + .for_each(|pixel| { + *pixel = Pixel::Empty(age); + }); } + + row += 1; } let mut col = 0; while col < self.width() { if !self.column_contains_galaxy(col) { - self.add_empty_column(col); - col += 2; - } else { - col += 1; + self.data.iter_mut().for_each(|row| { + row[col] = Pixel::Empty(age); + }); } + col += 1; } } - fn add_empty_row(&mut self, at_row: usize) { - self.data - .insert(at_row, (0..self.width()).map(|_| Pixel::Empty).collect()); - } - - fn add_empty_column(&mut self, at_col: usize) { - self.data.iter_mut().for_each(|row| row.insert(at_col, Pixel::Empty)); - } - fn row_contains_galaxy(&self, row: usize) -> bool { self.data[row].contains(&Pixel::Galaxy) } @@ -125,13 +145,26 @@ impl Image { self.data .iter() .enumerate() - .flat_map(|(row, row_pixels)| { + .flat_map(|(r, row_pixels)| { row_pixels .iter() .enumerate() - .filter_map(|(col, pixel)| { + .filter_map(|(c, pixel)| { if pixel == &Pixel::Galaxy { - Some(Coordinate::new(row, col)) + let coord = self.data[0..=r].iter().enumerate().fold( + Coordinate::new(0, 0), + |mut acc, (row, col_data)| { + if row == r { + acc = acc.add_col(col_data[0..c].iter().map(|p| p.get_length()).sum()); + } else { + acc = acc.add_row(col_data[c].get_length()); + }; + + acc + }, + ); + + Some(coord) } else { None } @@ -185,19 +218,28 @@ mod tests { #...#....."; #[test] - fn test_expansion() { + fn test_10x_expansion() { + let mut image = Image::from_str(EXAMPLE).expect("Failed to parse image"); + + image.expand(10); + + assert_eq!(1030, image.find_shortest_paths_sum()); + } + + #[test] + fn test_100x_expansion() { let mut image = Image::from_str(EXAMPLE).expect("Failed to parse image"); - image.expand(); + image.expand(100); - assert_eq!((image.height(), image.width()), (12, 13)); + assert_eq!(8410, image.find_shortest_paths_sum()); } #[test] fn solution_1() { let mut image = Image::from_str(EXAMPLE).expect("Failed to parse image"); - image.expand(); + image.expand(2); assert_eq!(374, image.find_shortest_paths_sum()); } From a1411b9828479384f5e273d7b58e4c83c733730e Mon Sep 17 00:00:00 2001 From: Mads Jensen Date: Fri, 25 Oct 2024 11:42:18 +0200 Subject: [PATCH 3/3] add comments and add solution to readme --- README.md | 2 +- bin/src/main.rs | 10 ++++------ lib/observatory/src/lib.rs | 30 +++++++++++++++++++++++++----- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 1bff869..c5e017b 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Links can be found in the table below: | [08](https://adventofcode.com/2023/day/8) | [lib/network_nodes](./lib/network_nodes/) | | [09](https://adventofcode.com/2023/day/9) | [lib/oasis](./lib/oasis/) | | [10](https://adventofcode.com/2023/day/10) | [lib/pipe_maze](./lib/pipe_maze/) | -| [11](https://adventofcode.com/2023/day/11) | 🕙TBD | +| [11](https://adventofcode.com/2023/day/11) | [lib/observatory](./lib/observatory/) | | [12](https://adventofcode.com/2023/day/12) | 🕙TBD | | [13](https://adventofcode.com/2023/day/13) | 🕙TBD | | [14](https://adventofcode.com/2023/day/14) | 🕙TBD | diff --git a/bin/src/main.rs b/bin/src/main.rs index 0c6210d..dcf263b 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs @@ -129,18 +129,16 @@ fn day10(input: String) { } fn day11(input: String) { - let image = Image::from_str(&input).expect("Failed to parse image"); + let mut image = Image::from_str(&input).expect("Failed to parse image"); - let mut image_1 = image.clone(); - image_1.expand(1); + image.resize(2); let paths_sum = image.find_shortest_paths_sum(); println!("Shortest paths sum: {paths_sum}"); - let mut image_2 = image; - image_2.expand(1_000_000); + image.resize(1_000_000); - let paths_sum = image_2.find_shortest_paths_sum(); + let paths_sum = image.find_shortest_paths_sum(); println!("Shortest paths sum 1mil: {paths_sum}"); } diff --git a/lib/observatory/src/lib.rs b/lib/observatory/src/lib.rs index a7c8456..57a6d9c 100644 --- a/lib/observatory/src/lib.rs +++ b/lib/observatory/src/lib.rs @@ -5,6 +5,7 @@ pub enum ObservatoryError { ParsePixel(char), } +/// A coordinate in the image #[derive(Debug)] pub struct Coordinate { row: usize, @@ -16,10 +17,12 @@ impl Coordinate { Self { row, col } } + /// Finds the shortest path to another coordinate pub const fn shortest_path_to(&self, other: &Self) -> usize { self.col.abs_diff(other.col) + self.row.abs_diff(other.row) } + /// Adds to the row part of the coordinate pub const fn add_row(&self, row: usize) -> Self { Self { row: self.row + row, @@ -27,6 +30,7 @@ impl Coordinate { } } + /// Adds to the column part of the coordinate pub const fn add_col(&self, col: usize) -> Self { Self { row: self.row, @@ -35,6 +39,10 @@ impl Coordinate { } } +/// A pixel in the image. +/// +/// The empty variant contains the length of the empty space. +/// #[derive(Debug, Clone, PartialEq, Eq)] pub enum Pixel { Empty(usize), @@ -42,6 +50,7 @@ pub enum Pixel { } impl Pixel { + /// Gets the "length" of the pixel pub const fn get_length(&self) -> usize { match self { Self::Empty(n) => *n, @@ -67,13 +76,14 @@ impl TryFrom for Pixel { impl Display for Pixel { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let pixel = match self { - Self::Empty(n) => format!("_.x{}_", n), + Self::Empty(n) => format!(".x{}", n), Self::Galaxy => "#".to_string(), }; write!(f, "{pixel}") } } +/// An image containing pixels #[derive(Clone)] pub struct Image { data: Vec>, @@ -101,7 +111,9 @@ impl Image { self.data[0].len() } - pub fn expand(&mut self, age: usize) { + /// Resizes the image according to the age + pub fn resize(&mut self, age: usize) { + // Resize rows that do not contain a galaxy (Empty rows) let mut row = 0; while row < self.height() { if !self.row_contains_galaxy(row) { @@ -117,6 +129,7 @@ impl Image { row += 1; } + // Resize columns that do not contain a galaxy (Empty columns) let mut col = 0; while col < self.width() { if !self.column_contains_galaxy(col) { @@ -128,10 +141,12 @@ impl Image { } } + /// Checks if a row contains a galaxy fn row_contains_galaxy(&self, row: usize) -> bool { self.data[row].contains(&Pixel::Galaxy) } + /// Checks if a column contains a galaxy fn column_contains_galaxy(&self, col: usize) -> bool { (0..self.height()).any(|row| { if let Some(pixel) = self.data[row].get(col) { @@ -141,16 +156,21 @@ impl Image { }) } + /// Finds the coordinates of all galaxies in the image fn find_galaxies(&self) -> Vec { + // Iterate over rows self.data .iter() .enumerate() .flat_map(|(r, row_pixels)| { + // Iterate the pixels in the row row_pixels .iter() .enumerate() .filter_map(|(c, pixel)| { + // If the pixel is a galaxy -> find the coordinate if pixel == &Pixel::Galaxy { + // Create a coordinate by summing the lengths of the pixels let coord = self.data[0..=r].iter().enumerate().fold( Coordinate::new(0, 0), |mut acc, (row, col_data)| { @@ -221,7 +241,7 @@ mod tests { fn test_10x_expansion() { let mut image = Image::from_str(EXAMPLE).expect("Failed to parse image"); - image.expand(10); + image.resize(10); assert_eq!(1030, image.find_shortest_paths_sum()); } @@ -230,7 +250,7 @@ mod tests { fn test_100x_expansion() { let mut image = Image::from_str(EXAMPLE).expect("Failed to parse image"); - image.expand(100); + image.resize(100); assert_eq!(8410, image.find_shortest_paths_sum()); } @@ -239,7 +259,7 @@ mod tests { fn solution_1() { let mut image = Image::from_str(EXAMPLE).expect("Failed to parse image"); - image.expand(2); + image.resize(2); assert_eq!(374, image.find_shortest_paths_sum()); }