From 7e0d58a8eae6eaed9735d5098f04c4fc09b9306e Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Tue, 12 Dec 2023 17:24:51 +0100 Subject: [PATCH] day 5 part 2 --- src/five.rs | 30 ++++++++++++++++++++++++++++++ src/main.rs | 1 + 2 files changed, 31 insertions(+) diff --git a/src/five.rs b/src/five.rs index da5a0d8..d6db548 100644 --- a/src/five.rs +++ b/src/five.rs @@ -92,6 +92,36 @@ fn seed_to_location(maps: &Vec, 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> { + 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> { let lines = load_input("./input/5.txt")?; let seeds = get_seeds(lines.get(0).unwrap().as_str()); diff --git a/src/main.rs b/src/main.rs index e14b0c2..540350e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,7 @@ fn main() { "4" => four::run(), "4p2" => four::run_part2(), "5" => five::run(), + "5p2" => five::run_part2(), _ => Ok(-1), };