y2024d20p2

This commit is contained in:
Fabian Schmidt 2024-12-20 14:12:20 +01:00
parent cd42646864
commit 985b88f0aa

View File

@ -6,26 +6,14 @@ use std::{
use itertools::Itertools; use itertools::Itertools;
pub fn process_part1(input: &str) -> u32 { pub fn process_part1(input: &str) -> u32 {
simulate_all(input, 100).values().copied().sum() simulate_all(input, 100, 2).values().copied().sum()
//simulate_all(input)
// .iter()
// .map(
// |(time_saved, count)| {
// if *time_saved >= 100 {
// *count
// } else {
// 0
// }
// },
// )
// .sum()
} }
pub fn process_part2(input: &str) -> u32 { pub fn process_part2(input: &str) -> u32 {
simulate_all(input, 100).values().copied().sum() simulate_all(input, 100, 20).values().copied().sum()
} }
fn simulate_all(input: &str, time_to_saved: usize) -> HashMap<u32, u32> { fn simulate_all(input: &str, time_to_save: usize, max_cheat_time: usize) -> HashMap<u32, u32> {
let mut start = (0, 0); let mut start = (0, 0);
let grid = input let grid = input
.lines() .lines()
@ -45,15 +33,12 @@ fn simulate_all(input: &str, time_to_saved: usize) -> HashMap<u32, u32> {
.collect_vec(); .collect_vec();
let no_cheat = simulate(&grid, start); let no_cheat = simulate(&grid, start);
let mut saved = HashMap::new(); let mut saved = HashMap::new();
for (tile_idx, tile) in no_cheat[..no_cheat.len() - time_to_saved] for (tile_idx, tile) in no_cheat[..no_cheat.len() - time_to_save].iter().enumerate() {
.iter() for (cheat_idx, cheat) in no_cheat[tile_idx..].iter().enumerate() {
.enumerate()
{
for (cheat_idx, cheat) in no_cheat[tile_idx + time_to_saved..].iter().enumerate() {
let manhattan = tile.0.abs_diff(cheat.0) + tile.1.abs_diff(cheat.1); let manhattan = tile.0.abs_diff(cheat.0) + tile.1.abs_diff(cheat.1);
if manhattan <= 2 { if manhattan <= max_cheat_time {
let time_saved = (time_to_saved + cheat_idx).saturating_sub(2); let time_saved = cheat_idx - manhattan;
if time_saved != 0 { if time_saved >= time_to_save {
saved saved
.entry(time_saved as u32) .entry(time_saved as u32)
.and_modify(|count| *count += 1) .and_modify(|count| *count += 1)
@ -110,7 +95,6 @@ fn simulate(grid: &[Vec<GridTile>], start: (usize, usize)) -> Vec<(usize, usize)
let arrived_reindeer = next_paths.remove(idx); let arrived_reindeer = next_paths.remove(idx);
arrived.push(arrived_reindeer); arrived.push(arrived_reindeer);
} }
//log_maze(&grid, maze_runner);
} }
} }
visited visited
@ -192,31 +176,6 @@ impl TryFrom<char> for GridTile {
} }
} }
fn log_maze(grid: &[Vec<GridTile>], maze_runner: &MazeRunner) {
for (yidx, row) in grid.iter().enumerate() {
for (xidx, tile) in row.iter().enumerate() {
let contains = {
maze_runner.visited.contains(&(xidx, yidx))
|| maze_runner.visited.contains(&(xidx, yidx))
|| maze_runner.visited.contains(&(xidx, yidx))
|| maze_runner.visited.contains(&(xidx, yidx))
};
if contains && !(*tile == GridTile::Start || *tile == GridTile::End) {
print!("O");
} else if *tile == GridTile::Wall {
print!("#");
} else if *tile == GridTile::Path {
print!(".");
} else if *tile == GridTile::Start {
print!("S");
} else if *tile == GridTile::End {
print!("E");
}
}
println!();
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::time::Instant; use std::time::Instant;
@ -245,7 +204,7 @@ mod tests {
fn part1() { fn part1() {
let now = Instant::now(); let now = Instant::now();
println!("Test 2:"); println!("Test 2:");
let result = simulate_all(INPUT, 0); let result = simulate_all(INPUT, 2, 2);
result result
.iter() .iter()
.sorted_by(|a, b| a.0.cmp(b.0)) .sorted_by(|a, b| a.0.cmp(b.0))
@ -253,13 +212,14 @@ mod tests {
println!("There are {count} cheats that saved {saved} picoseconds"); println!("There are {count} cheats that saved {saved} picoseconds");
}); });
println!("Ran in {}", get_elapsed_string(now.elapsed())); println!("Ran in {}", get_elapsed_string(now.elapsed()));
assert_eq!(result.values().copied().sum::<u32>(), 44);
} }
#[test] #[test]
fn part2() { fn part2() {
let now = Instant::now(); let now = Instant::now();
println!("Test 2:"); println!("Test 2:");
let result = simulate_all(INPUT, 2); let result = simulate_all(INPUT, 50, 20);
result result
.iter() .iter()
.sorted_by(|a, b| a.0.cmp(b.0)) .sorted_by(|a, b| a.0.cmp(b.0))
@ -267,5 +227,6 @@ mod tests {
println!("There are {count} cheats that saved {saved} picoseconds"); println!("There are {count} cheats that saved {saved} picoseconds");
}); });
println!("Ran in {}", get_elapsed_string(now.elapsed())); println!("Ran in {}", get_elapsed_string(now.elapsed()));
assert_eq!(result.values().copied().sum::<u32>(), 285);
} }
} }