2025 - Day 2 part 2 - rust
Some checks reported errors
continuous-integration/drone/push Build encountered an error

This commit is contained in:
kleph 2025-12-02 18:20:03 +01:00
parent 80608eab58
commit 8f2667bff3

50
2025/rust2_2/src/main.rs Normal file
View file

@ -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<u8>> = Vec::new();
let mut bad_id: Vec<u64> = 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<u64> = 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)
}