diff --git a/2025/02.rs b/2025/02.rs new file mode 100644 index 0000000..942b13a --- /dev/null +++ b/2025/02.rs @@ -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); +} diff --git a/2025/inputs/02_1.txt b/2025/inputs/02_1.txt new file mode 100644 index 0000000..92e56ed --- /dev/null +++ b/2025/inputs/02_1.txt @@ -0,0 +1 @@ +17330-35281,9967849351-9967954114,880610-895941,942-1466,117855-209809,9427633930-9427769294,1-14,311209-533855,53851-100089,104-215,33317911-33385573,42384572-42481566,43-81,87864705-87898981,258952-303177,451399530-451565394,6464564339-6464748782,1493-2439,9941196-10054232,2994-8275,6275169-6423883,20-41,384-896,2525238272-2525279908,8884-16221,968909030-969019005,686256-831649,942986-986697,1437387916-1437426347,8897636-9031809,16048379-16225280 diff --git a/2025/inputs/02_example.txt b/2025/inputs/02_example.txt new file mode 100644 index 0000000..a3f22ef --- /dev/null +++ b/2025/inputs/02_example.txt @@ -0,0 +1 @@ +11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124