diff --git a/2025/rust2_2/src/main.rs b/2025/rust2_2/src/main.rs new file mode 100644 index 0000000..1bd14e7 --- /dev/null +++ b/2025/rust2_2/src/main.rs @@ -0,0 +1,50 @@ +use std::fs::File; +use std::io::{BufRead, BufReader}; + +fn main() { + // let filename = "../2/input_example.txt"; + let filename = "../2/input.txt"; + let file = BufReader::new(File::open(filename).expect("Unable to open file")); + + let mut ranges: Vec> = Vec::new(); + let mut bad_id: Vec = Vec::new(); + + for line in file.lines() { + let wline: String = line.unwrap(); + let sranges: Vec<&str> = wline.split(',').collect(); + + for s in sranges { + let range: Vec = s.split('-').map(|x| x.parse().unwrap()).collect(); + for id in range[0]..range[1] + 1 { + let s = id.to_string().into_bytes(); + ranges.push(s); + } + } + } + println!("number of ranges to inspect {:?}", ranges.len()); + + for product_id in ranges { + for pivot in 1..(product_id.len() / 2) + 1 { + let mut bad: bool = true; + let mut complete = 0; + for cursor in 0..product_id.len() { + complete = (cursor + 1) % pivot; + if product_id[cursor % pivot] != product_id[cursor] { + bad = false; + break; + } + } + if bad && complete == 0 { + let num = str::from_utf8(&product_id).unwrap(); + bad_id.push(num.parse().unwrap()) + } + } + } + println!("bad ids {:?}", bad_id.len()); + bad_id.sort(); + bad_id.dedup(); + println!("dedup bad ids {:?}", bad_id.len()); + let sum: u64 = bad_id.iter().sum(); + + println!("{}", sum) +}