Clean up day20

This commit is contained in:
Adrian Groh 2024-12-20 13:31:25 +01:00
parent dfa6d05f59
commit 6dc6f04368
Signed by: Gobidev
GPG Key ID: 3AA3153E98B0D771

View File

@ -24,7 +24,7 @@ impl Direction {
vec![Up, Left, Down, Right] vec![Up, Left, Down, Right]
} }
fn turn_right(&self) -> Self { fn turn(&self) -> Self {
match self { match self {
Up => Right, Up => Right,
Left => Up, Left => Up,
@ -99,36 +99,34 @@ fn get_distances(
} }
fn part12(map: &[Vec<Tile>], path: &[IVec2], max_distance: i32) -> usize { fn part12(map: &[Vec<Tile>], 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() path.par_iter()
.map(|tile| { .map(|tile| {
Direction::all() Direction::all()
.iter() .iter()
.map(|direction| { .map(|direction| {
(1..=max_distance) combinations
.map(|i| { .iter()
(0..=(max_distance - i)) .filter(|(i, j)| {
.filter(|j| { let new_tile_pos =
let new_tile_pos = tile tile + i * direction.to_vec() + j * direction.turn().to_vec();
+ i * direction.to_vec()
+ j * direction.turn_right().to_vec();
map.get(new_tile_pos.x as usize) map.get(new_tile_pos.x as usize)
.and_then(|l| l.get(new_tile_pos.y as usize)) .and_then(|l| l.get(new_tile_pos.y as usize))
.map_or(false, |t| { .map_or(false, |t| {
!t.tiletype t.distance
&& t.distance >= map[tile.x as usize][tile.y as usize].distance
>= map[tile.x as usize][tile.y as usize]
.distance
+ 100 + 100
+ i as usize + *i as usize
+ *j as usize + *j as usize
&& !t.tiletype
}) })
}) })
.count() .count()
}) })
.sum::<usize>() .sum::<usize>()
}) })
.sum::<usize>()
})
.sum() .sum()
} }