From 0a709e9309364823e77754463f529c04a88714fd Mon Sep 17 00:00:00 2001 From: Lennard Brinkhaus Date: Mon, 11 Dec 2023 14:21:29 +0100 Subject: [PATCH] refactor: some stuff --- src/day11.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/day11.rs b/src/day11.rs index ca0f4fc..b1991c6 100644 --- a/src/day11.rs +++ b/src/day11.rs @@ -17,8 +17,8 @@ pub fn execute_task02(content: &str) { let command_steps = solve_02(content).unwrap(); let duration = start.elapsed(); - //assert_eq!(957, command_steps); - println!("Day11 - Task02 - Duration: {duration:?} - SUm of closest million Points: {}", command_steps) + assert_eq!(678728808158, command_steps); + println!("Day11 - Task02 - Duration: {duration:?} - Sum of closest million Points: {}", command_steps) } @@ -34,6 +34,10 @@ fn solve(content: &str, expansion: usize) -> anyhow::Result { let height = content.lines().count(); let width = content.lines().next().unwrap().chars().count(); + let mut empty_y: Vec = (0..height).collect(); + let mut empty_x: Vec = (0..width).collect(); + + // Find all Galaxies let mut galaxies = content .lines() .enumerate() @@ -51,9 +55,8 @@ fn solve(content: &str, expansion: usize) -> anyhow::Result { ) .collect::>(); - let mut empty_y: Vec = (0..height).collect(); - let mut empty_x: Vec = (0..width).collect(); + // TODO move this shit in the first loop but ownership and move.... galaxies .iter() .for_each(|(y, x)| { @@ -61,13 +64,15 @@ fn solve(content: &str, expansion: usize) -> anyhow::Result { empty_x.retain(|value| *value != *x); }); + // Expand all cords galaxies = galaxies .iter() .map(|(y, x)| { - (y + (empty_y.iter().filter(|filler| filler < &y).count() * (expansion - 1)), x + (empty_x.iter().filter(|filler| filler < &x).count() * (expansion - 1))) + (y + (empty_y.iter().take_while(|filler| filler < &y).count() * (expansion - 1)), x + (empty_x.iter().take_while(|filler| filler < &x).count() * (expansion - 1))) }) .collect(); + // Calculate diff and sum them up Ok(galaxies.clone().iter() .combinations(2) .map(|data| (data[0], data[1]))