Compare commits
2 Commits
d7c0ce9190
...
1074c8d5ee
| Author | SHA1 | Date |
|---|---|---|
|
|
1074c8d5ee | |
|
|
370a2bc1e5 |
|
|
@ -121,20 +121,8 @@ 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 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>>());
|
|
||||||
}
|
|
||||||
debug!("New seeds: {:?}", new_seeds);
|
|
||||||
|
|
||||||
dataset.seeds = new_seeds;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut min_location = u64::MAX;
|
let mut min_location = u64::MAX;
|
||||||
for seed in dataset.seeds {
|
for seed in dataset.seeds {
|
||||||
let mut stage = "seed";
|
let mut stage = "seed";
|
||||||
|
|
@ -147,8 +135,27 @@ pub fn solve(lines: Vec<String>, part: Part) -> u64 {
|
||||||
min_location = min_location.min(value);
|
min_location = min_location.min(value);
|
||||||
info!("Final stage seed {} -> location {}", seed, value);
|
info!("Final stage seed {} -> location {}", seed, value);
|
||||||
}
|
}
|
||||||
|
return min_location;
|
||||||
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue