refactor: some stuff

This commit is contained in:
Lennard Brinkhaus 2023-12-11 14:21:29 +01:00
parent f1c0c453d3
commit 0a709e9309

View File

@ -17,8 +17,8 @@ pub fn execute_task02(content: &str) {
let command_steps = solve_02(content).unwrap(); let command_steps = solve_02(content).unwrap();
let duration = start.elapsed(); let duration = start.elapsed();
//assert_eq!(957, command_steps); assert_eq!(678728808158, command_steps);
println!("Day11 - Task02 - Duration: {duration:?} - SUm of closest million Points: {}", 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<usize> {
let height = content.lines().count(); let height = content.lines().count();
let width = content.lines().next().unwrap().chars().count(); let width = content.lines().next().unwrap().chars().count();
let mut empty_y: Vec<usize> = (0..height).collect();
let mut empty_x: Vec<usize> = (0..width).collect();
// Find all Galaxies
let mut galaxies = content let mut galaxies = content
.lines() .lines()
.enumerate() .enumerate()
@ -51,9 +55,8 @@ fn solve(content: &str, expansion: usize) -> anyhow::Result<usize> {
) )
.collect::<Vec<(usize, usize)>>(); .collect::<Vec<(usize, usize)>>();
let mut empty_y: Vec<usize> = (0..height).collect();
let mut empty_x: Vec<usize> = (0..width).collect();
// TODO move this shit in the first loop but ownership and move....
galaxies galaxies
.iter() .iter()
.for_each(|(y, x)| { .for_each(|(y, x)| {
@ -61,13 +64,15 @@ fn solve(content: &str, expansion: usize) -> anyhow::Result<usize> {
empty_x.retain(|value| *value != *x); empty_x.retain(|value| *value != *x);
}); });
// Expand all cords
galaxies = galaxies galaxies = galaxies
.iter() .iter()
.map(|(y, x)| { .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(); .collect();
// Calculate diff and sum them up
Ok(galaxies.clone().iter() Ok(galaxies.clone().iter()
.combinations(2) .combinations(2)
.map(|data| (data[0], data[1])) .map(|data| (data[0], data[1]))