Some checks reported errors
continuous-integration/drone/push Build encountered an error
50 lines
1.6 KiB
Rust
50 lines
1.6 KiB
Rust
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)
|
|
}
|