day5 part 2

This commit is contained in:
pasterp 2023-12-12 10:03:08 +01:00
parent 48cfa80dec
commit 370a2bc1e5
1 changed files with 32 additions and 25 deletions

View File

@ -121,34 +121,41 @@ pub fn solve(lines: Vec<String>, part: Part) -> u64 {
); );
} }
if let Part::Two = part { match part {
// Convert the seeds to the new format ! Part::One => {
let original_seeds = dataset.seeds; let mut min_location = u64::MAX;
let mut new_seeds : Vec<u64> = Vec::new(); for seed in dataset.seeds {
let mut stage = "seed";
for chunk in original_seeds.chunks_exact(2) { let mut value = seed;
let range = chunk[0]..(chunk[0]+chunk[1]); while let Some(map) = dataset.maps.get(stage) {
// FIXME : don't do that it will eat all your memory: new_seeds.append(&mut range.collect::<Vec<u64>>()); 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); Part::Two => {
let mut min_location = u64::MAX;
dataset.seeds = new_seeds; 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 min_location = u64::MAX; let mut stage = "seed";
for seed in dataset.seeds { let mut value = seed;
let mut stage = "seed"; while let Some(map) = dataset.maps.get(stage) {
let mut value = seed; debug!("Stage {} -> {}", stage, map.target_domain);
while let Some(map) = dataset.maps.get(stage) { value = map.map(value);
debug!("Stage {} -> {}", stage, map.target_domain); 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)] #[cfg(test)]