feat: day 5 part 2 (this took <10mins on the 128 core uni compute server)

This commit is contained in:
Ashhhleyyy 2023-12-06 23:51:29 +00:00
parent 39620ac926
commit 8cb6a0fee2
Signed by: ash
GPG key ID: 83B789081A0878FB

View file

@ -1,4 +1,5 @@
use aoc_2022::prelude::*; use aoc_2022::prelude::*;
use color_eyre::eyre::ContextCompat;
use rayon::prelude::*; use rayon::prelude::*;
type Input = Almanac; type Input = Almanac;
@ -53,13 +54,32 @@ fn day_5(mut input: Input) -> Result<()> {
} }
} }
current_position current_position
}).min(); }).min().wrap_err("missing result")?;
println!("{result:?}"); println!("{result}");
// Part 2 // Part 2
println!("Part two: "); println!("Part two: ");
let result = input.seeds.chunks_exact(2).flat_map(|range| {
let range_start = range[0];
let range_length = range[1];
(0..range_length).map(move |s| s + range_start)
}).par_bridge().map(|seed| {
let mut current_position = seed;
for ranges in &input.maps {
let range = ranges.iter().filter(|(_, src_start, length)| {
current_position >= *src_start && (current_position - *src_start) < *length
}).next();
if let Some((dest_start, src_start, _)) = range {
current_position = dest_start + (current_position - src_start);
}
}
current_position
}).min().wrap_err("there should be a result")?;
println!("{result}");
// let result = input.seeds.chunks_exact(2).map(|range| { // let result = input.seeds.chunks_exact(2).map(|range| {
// let range_start = range[0]; // let range_start = range[0];
// let range_length = range[1]; // let range_length = range[1];