37 lines
982 B
Rust
37 lines
982 B
Rust
---cargo
|
|
---
|
|
|
|
static CONTENT: &'static str = include_str!("./inputs/02_1.txt");
|
|
|
|
fn main() {
|
|
let ranges: Vec<_> = CONTENT.split(',').map(|l| l.trim()).filter(|l| !l.is_empty()).map(|l| {
|
|
let (raw_start, raw_end) = l.split_once('-').unwrap();
|
|
let start: usize = raw_start.parse().unwrap();
|
|
let end: usize = raw_end.parse().unwrap();
|
|
|
|
start..=end
|
|
}).collect();
|
|
|
|
println!("Ranges: {:?}", ranges);
|
|
|
|
let mut result = 0;
|
|
for r in ranges.iter() {
|
|
for v in r.clone() {
|
|
let len = v.ilog10() + 1;
|
|
if len % 2 != 0 {
|
|
continue;
|
|
}
|
|
|
|
let first_half = v / 10_usize.pow(len/2);
|
|
let second_half = v - (first_half * 10_usize.pow(len/2));
|
|
|
|
if first_half == second_half {
|
|
result += v;
|
|
println!("Invalid {} - Halves ({}, {})", v, first_half, second_half);
|
|
}
|
|
}
|
|
}
|
|
|
|
println!("Result: {}", result);
|
|
}
|