From 5c4a0134396597e9a3837fb1693d7219b109300a Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Thu, 14 Dec 2023 21:00:04 -0500 Subject: [PATCH] Day 5 part 2 in progress --- day_5/src/main.rs | 68 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 62 insertions(+), 6 deletions(-) diff --git a/day_5/src/main.rs b/day_5/src/main.rs index e0ff566..6197cd9 100644 --- a/day_5/src/main.rs +++ b/day_5/src/main.rs @@ -6,6 +6,21 @@ use std::{io::prelude::*, fs::File, path::Path, collections::HashMap }; // DATA STRUCTS ///////////////////////////////////////////////////////////////////// +#[derive(Clone, Debug)] +struct SeedRange +{ + start: i64, + length: i64, +} + +impl SeedRange +{ + fn new(start: i64, length: i64) -> SeedRange + { + SeedRange { start, length } + } +} + #[derive(Clone, Debug)] struct SubMap { @@ -82,7 +97,7 @@ impl Map fn main() { let input = load_data("data/input"); - let (seeds, maps) = parse_input(&input); + let (seeds, maps, seed_ranges) = parse_input(&input); // TEST STUFF // println!("SEEDS: {:#?}, MAPS: {:#?}", seeds, maps); @@ -97,6 +112,35 @@ fn main() let location = find_closest_location(&seeds, &maps); println!("Closest location: {}", location); + + let second_loc = find_closest_from_range(&seed_ranges, &maps); + println!("Closest location from range: {}", second_loc); +} + +fn find_closest_from_range(seed_ranges: &Vec, maps: &Vec) -> i64 +{ + let mut closest_location = i64::MAX; + + for seed_range in seed_ranges + { + println!("Checking seed range: start: {}, length: {}", seed_range.start, seed_range.length); + + let start = seed_range.start; + let end = seed_range.start + seed_range.length + 1; + for seed in start..end + { + let location = find_seed_location(seed, maps); + + if location < closest_location + { + closest_location = location; + println!("New lowest location: {}", closest_location); + // best_seed = *seed; + } + } + } + + closest_location } fn find_closest_location(seeds: &Vec, maps: &Vec) -> i64 @@ -125,7 +169,7 @@ fn find_seed_location(seed: i64, maps: &Vec) -> i64 let mut current_value = seed; for map in maps { - println!("Mapping {}...", map.name); + // println!("Mapping {}...", map.name); current_value = map.map_input(current_value); } @@ -135,9 +179,10 @@ fn find_seed_location(seed: i64, maps: &Vec) -> i64 ///////////////////////////////////////////////////////////////////// // INPUT PARSING ///////////////////////////////////////////////////////////////////// -fn parse_input(input: &str) -> (Vec, Vec) // String is the map name +fn parse_input(input: &str) -> (Vec, Vec, Vec) // String is the map name { let mut seeds: Vec = Vec::new(); + let mut seed_ranges: Vec = Vec::new(); let mut maps: Vec = Vec::new(); let mut current_map = ""; @@ -153,11 +198,22 @@ fn parse_input(input: &str) -> (Vec, Vec) // String is the map name // SEEDS if line.contains("seeds:") { - println!("Parseing seed input..."); + println!("Parsing seed input..."); + let mut start_value: i64 = -1; for val in line.trim_start_matches("seeds:").trim().split(" ") { let num = val.parse::().expect(&format!("Failed to parse seed value: {}", val)); seeds.push(num); + + if start_value == -1 + { + start_value = num; + } + else + { + seed_ranges.push(SeedRange::new(start_value, num)); + start_value = -1; + } } continue; @@ -179,7 +235,7 @@ fn parse_input(input: &str) -> (Vec, Vec) // String is the map name // NEW SUB MAP let mut sub_map = SubMap::new(); let vals: Vec<&str> = line.trim().split(" ").collect(); - println!("Parsing sub map: {}", sub_map_idx); + // println!("Parsing sub map: {}", sub_map_idx); if vals.len() < 3 { @@ -204,7 +260,7 @@ fn parse_input(input: &str) -> (Vec, Vec) // String is the map name // } } - (seeds, maps) + (seeds, maps, seed_ranges) }