feat: day 5 part 2 (this took <10mins on the 128 core uni compute server)
This commit is contained in:
parent
39620ac926
commit
8cb6a0fee2
1 changed files with 22 additions and 2 deletions
|
@ -1,4 +1,5 @@
|
|||
use aoc_2022::prelude::*;
|
||||
use color_eyre::eyre::ContextCompat;
|
||||
use rayon::prelude::*;
|
||||
|
||||
type Input = Almanac;
|
||||
|
@ -53,13 +54,32 @@ fn day_5(mut input: Input) -> Result<()> {
|
|||
}
|
||||
}
|
||||
current_position
|
||||
}).min();
|
||||
}).min().wrap_err("missing result")?;
|
||||
|
||||
println!("{result:?}");
|
||||
println!("{result}");
|
||||
|
||||
// Part 2
|
||||
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 range_start = range[0];
|
||||
// let range_length = range[1];
|
||||
|
|
Loading…
Reference in a new issue