2025 - Day 2 part 1 - rust
Some checks reported errors
continuous-integration/drone/push Build encountered an error
Some checks reported errors
continuous-integration/drone/push Build encountered an error
This commit is contained in:
parent
5ae76a626b
commit
80608eab58
1 changed files with 50 additions and 0 deletions
50
2025/rust2/src/main.rs
Normal file
50
2025/rust2/src/main.rs
Normal 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<String> = Vec::new();
|
||||
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();
|
||||
// println!("{:?}", range);
|
||||
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());
|
||||
// println!("{:?}", ranges);
|
||||
|
||||
for product_id in ranges {
|
||||
if product_id.len() % 2 == 1 {
|
||||
continue;
|
||||
}
|
||||
let pivot = product_id.len() / 2;
|
||||
let mut bad: bool = true;
|
||||
for i in 0..pivot {
|
||||
if product_id[i] != product_id[pivot + i] {
|
||||
bad = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if bad {
|
||||
let num = str::from_utf8(&product_id).unwrap();
|
||||
bad_id.push(num.parse().unwrap())
|
||||
}
|
||||
}
|
||||
// println!("bad ids {:?}", bad_id);
|
||||
let sum: u64 = bad_id.iter().sum();
|
||||
|
||||
println!("{}", sum)
|
||||
}
|
||||
Loading…
Reference in a new issue