Compare commits

..

No commits in common. "1074c8d5ee9bcb45543c4150b52cd1b66c225ca3" and "d7c0ce9190037039aebb45afbbbeed8cc2726e40" have entirely different histories.

1 changed files with 26 additions and 33 deletions

View File

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