From 370a2bc1e592269eea86e3d3a0c0cab1201ad5a9 Mon Sep 17 00:00:00 2001 From: pasterp Date: Tue, 12 Dec 2023 10:03:08 +0100 Subject: [PATCH] day5 part 2 --- src/day5/mod.rs | 57 +++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/src/day5/mod.rs b/src/day5/mod.rs index 27a2033..8d1e71e 100644 --- a/src/day5/mod.rs +++ b/src/day5/mod.rs @@ -121,34 +121,41 @@ pub fn solve(lines: Vec, part: Part) -> u64 { ); } - if let Part::Two = part { - // Convert the seeds to the new format ! - let original_seeds = dataset.seeds; - let mut new_seeds : Vec = Vec::new(); - - for chunk in original_seeds.chunks_exact(2) { - let range = chunk[0]..(chunk[0]+chunk[1]); - // FIXME : don't do that it will eat all your memory: new_seeds.append(&mut range.collect::>()); + match part { + Part::One => { + let mut min_location = u64::MAX; + for seed in dataset.seeds { + let mut stage = "seed"; + let mut value = seed; + while let Some(map) = dataset.maps.get(stage) { + debug!("Stage {} -> {}", stage, map.target_domain); + value = map.map(value); + stage = &map.target_domain; + } + min_location = min_location.min(value); + info!("Final stage seed {} -> location {}", seed, value); + } + return min_location; } - debug!("New seeds: {:?}", new_seeds); - - dataset.seeds = new_seeds; - } - - let mut min_location = u64::MAX; - for seed in dataset.seeds { - let mut stage = "seed"; - let mut value = seed; - while let Some(map) = dataset.maps.get(stage) { - debug!("Stage {} -> {}", stage, map.target_domain); - value = map.map(value); - stage = &map.target_domain; + Part::Two => { + let mut min_location = u64::MAX; + for chunk in dataset.seeds.chunks_exact(2) { + info!("Computing range {} -> {}", chunk[0], chunk[0] + chunk[1]); + for seed in chunk[0]..(chunk[0] + chunk[1]) { + let mut stage = "seed"; + let mut value = seed; + while let Some(map) = dataset.maps.get(stage) { + debug!("Stage {} -> {}", stage, map.target_domain); + value = map.map(value); + stage = &map.target_domain; + } + min_location = min_location.min(value); + trace!("Final stage seed {} -> location {}", seed, value); + } + } + return min_location; } - min_location = min_location.min(value); - info!("Final stage seed {} -> location {}", seed, value); } - - min_location } #[cfg(test)]