From 44d402849a8679aff702bdd9c7668a5f1005460e Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Wed, 13 Dec 2023 21:25:37 +0100 Subject: [PATCH] A small refactor --- src/days/day11.rs | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/days/day11.rs b/src/days/day11.rs index 22a5293..8eadb37 100644 --- a/src/days/day11.rs +++ b/src/days/day11.rs @@ -27,7 +27,7 @@ impl FromStr for Chart { } impl Chart { - fn detect_doubles(&mut self) { + fn detect_empties(&mut self) { for x in 0..self.stride { if self.space.iter().skip(x).step_by(self.stride).all(|c| *c == '.') { self.empty_columns.insert(x); @@ -51,47 +51,43 @@ impl Chart { vec } - fn distance(&self, c1: &(usize, usize), c2: &(usize, usize), extra_distance: u64) -> u64 { + fn distance(&self, c1: &(usize, usize), c2: &(usize, usize), empty_distance: u64) -> u64 { let mut steps = 0; for x in (min(c1.0, c2.0)..=max(c1.0, c2.0)).into_iter().skip(1) { - steps += 1; if self.empty_columns.contains(&x) { - steps += extra_distance; + steps += empty_distance; + } else { + steps += 1; } } for y in (min(c1.1, c2.1)..=max(c1.1, c2.1)).into_iter().skip(1) { - steps += 1; if (self.empty_rows).contains(&y) { - steps += extra_distance; + steps += empty_distance; + } else { + steps += 1; } } steps } } -pub fn one() -> Result { +pub fn solve(empty_distance: u64) -> Result { let mut chart = read_into::(Path::new("resources/input11.txt"))?; - chart.detect_doubles(); + chart.detect_empties(); let galaxy_coords = chart.find_galaxys(); let mut sum: u64 = 0; for (i, c1) in galaxy_coords.iter().enumerate() { for c2 in galaxy_coords.iter().skip(i+1) { - sum += chart.distance(c1, c2, 2-1); + sum += chart.distance(c1, c2, empty_distance); } } - Ok(sum as u64) + Ok(sum) } +pub fn one() -> Result { + solve(2) +} pub fn two() -> Result { - let mut chart = read_into::(Path::new("resources/input11.txt"))?; - chart.detect_doubles(); - let galaxy_coords = chart.find_galaxys(); - let mut sum: u64 = 0; - for (i, c1) in galaxy_coords.iter().enumerate() { - for c2 in galaxy_coords.iter().skip(i+1) { - sum += chart.distance(c1, c2, 1000000-1); - } - } - Ok(sum as u64) + solve(1000000) } \ No newline at end of file