diff --git a/aoc-5/src/main.rs b/aoc-5/src/main.rs index da4d014..052660e 100644 --- a/aoc-5/src/main.rs +++ b/aoc-5/src/main.rs @@ -2,8 +2,8 @@ use std::fs; use regex::Regex; fn main() { println!("Lets begin, day 5"); - let file_path = "src/input.txt"; - // let file_path = "src/example.txt"; + // let file_path = "src/input.txt"; + let file_path = "src/example.txt"; let re = Regex::new(r"(?\d*)-(?\d*)").unwrap(); let mut ranges: Vec<(i64, i64)> = Vec::new(); @@ -51,16 +51,61 @@ fn main() { } - println!("Fresh Products: {fresh:?}"); - println!("Spoiled Products: {spoiled:?}"); + // println!("Fresh Products: {fresh:?}"); + // println!("Spoiled Products: {spoiled:?}"); let fresh_count = fresh.len(); let spoiled_count = spoiled.len(); println!("Fresh Count: {fresh_count:?}"); println!("Spoiled Count: {spoiled_count:?}"); + // part 2 logic - how many fresh id's are there in total + + // brute force will take literally forever + // sort all the ranges based on their start number + // then go through all of them and if start is less than the last ranges end make it one more than the last ranges end + // then calculate the difference between each range and add them up for each range (end - start + 1) + + let mut all_fresh: Vec = Vec::new(); + // for range in &ranges{ + // for id in range.0..=range.1 { + // if !all_fresh.contains(&id) { + // all_fresh.push(id); + // } + // } + // print!("."); + // } + //sorting time + println!("{ranges:?}"); + ranges.sort_by(|a,b| a.0.cmp(&b.0)); + println!("{ranges:?}"); + + let mut last_end = -1; //? + for mut range in &mut ranges{ + if range.0 < last_end{ + range.0 = last_end + 1; + } + last_end = range.1; + } + // let meow = &ranges; + println!("{ranges:?}"); + let mut all_count:i64 = 0; + for mut range in &mut ranges{ + if range.0 > range.1 { + // invalid range, disregard + } + else { + all_count += range.1 - range.0 + 1; + } + + } + // let all_count = all_fresh.len(); + println!("All fresh id count: {all_count:?}"); + + // too low: 330684820916269 + // too high: 348115621205555 }