From 8cb6a0fee2bb7d213db1063ecc5e30e86adc8c11 Mon Sep 17 00:00:00 2001 From: Ashhhleyyy Date: Wed, 6 Dec 2023 23:51:29 +0000 Subject: [PATCH] feat: day 5 part 2 (this took <10mins on the 128 core uni compute server) --- src/bin/day_5.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/bin/day_5.rs b/src/bin/day_5.rs index e2c791c..600b8a7 100644 --- a/src/bin/day_5.rs +++ b/src/bin/day_5.rs @@ -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];