Compare commits

..

2 Commits

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 {
// Convert the seeds to the new format !
let original_seeds = dataset.seeds;
let mut new_seeds : Vec<u64> = 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::<Vec<u64>>());
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)]