day 5 part 2
This commit is contained in:
parent
f0c1f6aa9f
commit
7e0d58a8ea
30
src/five.rs
30
src/five.rs
@ -92,6 +92,36 @@ fn seed_to_location(maps: &Vec<TranslationMap>, num: u64) -> u64 {
|
||||
return result;
|
||||
}
|
||||
|
||||
fn pair_seeds(seeds: &[u64]) -> Vec<(u64, u64)> {
|
||||
let mut output: Vec<(u64, u64)> = Vec::new();
|
||||
|
||||
for i in 0..seeds.len() {
|
||||
if i % 2 == 1{
|
||||
continue;
|
||||
}
|
||||
|
||||
output.push((seeds[i],seeds[i+1]));
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
pub fn run_part2() -> Result<i32, Box<dyn std::error::Error>> {
|
||||
let lines = load_input("./input/5.txt")?;
|
||||
let seeds = get_seeds(lines.get(0).unwrap().as_str());
|
||||
let maps = parse_input(&lines[1..]);
|
||||
let pairs = pair_seeds(&seeds);
|
||||
|
||||
let res = pairs
|
||||
.iter()
|
||||
.flat_map(|e| e.0..(e.0 + e.1))
|
||||
.map(|e| seed_to_location(&maps, e))
|
||||
.min()
|
||||
.unwrap();
|
||||
|
||||
Ok(res.try_into().unwrap())
|
||||
}
|
||||
|
||||
pub fn run() -> Result<i32, Box<dyn std::error::Error>> {
|
||||
let lines = load_input("./input/5.txt")?;
|
||||
let seeds = get_seeds(lines.get(0).unwrap().as_str());
|
||||
|
@ -21,6 +21,7 @@ fn main() {
|
||||
"4" => four::run(),
|
||||
"4p2" => four::run_part2(),
|
||||
"5" => five::run(),
|
||||
"5p2" => five::run_part2(),
|
||||
_ => Ok(-1),
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user