From 6dc6f0436898c11e4c19d7b5ee67d1bf3c5b9d63 Mon Sep 17 00:00:00 2001 From: Adrian Groh Date: Fri, 20 Dec 2024 13:31:25 +0100 Subject: [PATCH] Clean up day20 --- day20/src/main.rs | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/day20/src/main.rs b/day20/src/main.rs index 7fcb997..932a62c 100644 --- a/day20/src/main.rs +++ b/day20/src/main.rs @@ -24,7 +24,7 @@ impl Direction { vec![Up, Left, Down, Right] } - fn turn_right(&self) -> Self { + fn turn(&self) -> Self { match self { Up => Right, Left => Up, @@ -99,33 +99,31 @@ fn get_distances( } fn part12(map: &[Vec], path: &[IVec2], max_distance: i32) -> usize { + let combinations: Vec<(i32, i32)> = (1..=max_distance) + .flat_map(|i| (0..=(max_distance - i)).map(move |j| (i, j))) + .collect(); path.par_iter() .map(|tile| { Direction::all() .iter() .map(|direction| { - (1..=max_distance) - .map(|i| { - (0..=(max_distance - i)) - .filter(|j| { - let new_tile_pos = tile - + i * direction.to_vec() - + j * direction.turn_right().to_vec(); - map.get(new_tile_pos.x as usize) - .and_then(|l| l.get(new_tile_pos.y as usize)) - .map_or(false, |t| { - !t.tiletype - && t.distance - >= map[tile.x as usize][tile.y as usize] - .distance - + 100 - + i as usize - + *j as usize - }) + combinations + .iter() + .filter(|(i, j)| { + let new_tile_pos = + tile + i * direction.to_vec() + j * direction.turn().to_vec(); + map.get(new_tile_pos.x as usize) + .and_then(|l| l.get(new_tile_pos.y as usize)) + .map_or(false, |t| { + t.distance + >= map[tile.x as usize][tile.y as usize].distance + + 100 + + *i as usize + + *j as usize + && !t.tiletype }) - .count() }) - .sum::() + .count() }) .sum::() })