Implement 2025 - Day 2 - Part 1
This commit is contained in:
36
2025/02.rs
Normal file
36
2025/02.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
---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);
|
||||
}
|
||||
Reference in New Issue
Block a user