Day 20 (another small refactor)

This commit is contained in:
sepia 2024-12-20 18:06:41 -06:00
parent 9ca870d1bb
commit 9df3c2662d
1 changed files with 11 additions and 4 deletions

View File

@ -12,14 +12,21 @@ pub fn day20() {
let walkable_tiles: Vec<(usize, usize)> = distance_map let walkable_tiles: Vec<(usize, usize)> = distance_map
.enumerate() .enumerate()
.filter_map(|(x, y, &d)| if d.is_some() { Some((x, y)) } else { None }) // get all tiles that have paths to the endpoint
.filter_map(|(x, y, &d)| match d {
Some(_) => Some((x, y)),
None => None,
})
.collect(); .collect();
let tile_pairs = walkable_tiles let all_cheats = walkable_tiles
.iter() .iter()
// for all pairs of walkable tiles
.cartesian_product(walkable_tiles.iter()) .cartesian_product(walkable_tiles.iter())
.filter(|(&(ax, ay), &(bx, by))| !(ax == bx && ay == by)); // excluding tiles that are the same tile
let all_cheats = tile_pairs .filter(|(&(ax, ay), &(bx, by))| !(ax == bx && ay == by))
// excluding tiles that are too far to cheat between
.filter(|(&(ax, ay), &(bx, by))| taxicab_distance(ax, ay, bx, by) <= CHEAT_DURATION) .filter(|(&(ax, ay), &(bx, by))| taxicab_distance(ax, ay, bx, by) <= CHEAT_DURATION)
// get the time saved by going from a to b
.map(|(&(ax, ay), &(bx, by))| { .map(|(&(ax, ay), &(bx, by))| {
let taxi_distance = taxicab_distance(ax, ay, bx, by) as i64; let taxi_distance = taxicab_distance(ax, ay, bx, by) as i64;
let a_distance_to_end = distance_map.get(ax, ay).unwrap().unwrap() as i64; let a_distance_to_end = distance_map.get(ax, ay).unwrap().unwrap() as i64;