diff --git a/src/day20.rs b/src/day20.rs index c05e0a8..ef68cd0 100644 --- a/src/day20.rs +++ b/src/day20.rs @@ -12,14 +12,21 @@ pub fn day20() { let walkable_tiles: Vec<(usize, usize)> = distance_map .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(); - let tile_pairs = walkable_tiles + let all_cheats = walkable_tiles .iter() + // for all pairs of walkable tiles .cartesian_product(walkable_tiles.iter()) - .filter(|(&(ax, ay), &(bx, by))| !(ax == bx && ay == by)); - let all_cheats = tile_pairs + // excluding tiles that are the same tile + .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) + // get the time saved by going from a to b .map(|(&(ax, ay), &(bx, by))| { 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;