Advent of Code solutions

Day 2

bwc9876.dev 36e9353c 9eaca333

verified
+57 -5
+1
Cargo.lock
··· 338 338 dependencies = [ 339 339 "advent_core", 340 340 "macros", 341 + "rayon", 341 342 "utils", 342 343 ]
+1
years/2025/Cargo.toml
··· 7 7 [dependencies] 8 8 advent_core = { path = "../../advent_core" } 9 9 macros = { path = "../../macros" } 10 + rayon = "1.11.0" 10 11 utils = { path = "../../utils" }
+49 -5
years/2025/src/day_2.rs
··· 1 + use std::ops::RangeInclusive; 1 2 2 3 use advent_core::{Day, day_stuff, ex_for_day}; 4 + use utils::num::{num_digits, split_num_at, split_num_once}; 3 5 4 6 pub struct Day2; 5 7 6 8 impl Day for Day2 { 9 + day_stuff!(2, "1227775554", "4174379265", Vec<RangeInclusive<usize>>); 7 10 8 - day_stuff!(2, "", ""); 11 + fn part_1(input: Self::Input) -> Option<String> { 12 + let ans = input 13 + .into_iter() 14 + .flat_map(|r| { 15 + r.into_iter().filter(|x| { 16 + let (l, r) = split_num_once(*x); 17 + l == r 18 + }) 19 + }) 20 + .sum::<usize>(); 9 21 10 - fn part_1(_input: Self::Input) -> Option<String> { 11 - None 22 + Some(ans.to_string()) 12 23 } 13 24 14 - fn part_2(_input: Self::Input) -> Option<String> { 15 - None 25 + fn part_2(input: Self::Input) -> Option<String> { 26 + let ans = input 27 + .into_iter() 28 + .flat_map(|r| { 29 + r.into_iter().filter(|x| { 30 + let digs = num_digits(*x); 31 + (2..=digs).into_iter().filter(|n| digs % n == 0).any(|n| { 32 + let mut parts = Vec::with_capacity(n); 33 + let mut current = *x; 34 + let split_at = digs / n; 35 + for _ in 0..n { 36 + let (rest, part) = split_num_at(current, split_at as u32); 37 + parts.push(part); 38 + current = rest; 39 + } 40 + parts 41 + .first() 42 + .copied() 43 + .map_or(false, |first| parts.into_iter().all(|x| x == first)) 44 + }) 45 + }) 46 + }) 47 + .sum::<usize>(); 48 + 49 + Some(ans.to_string()) 50 + } 51 + 52 + fn parse_input(input: &str) -> Self::Input { 53 + input 54 + .split(',') 55 + .map(|r| { 56 + let (l, r) = r.split_once('-').unwrap(); 57 + (l.trim().parse::<usize>().unwrap())..=(r.trim().parse::<usize>().unwrap()) 58 + }) 59 + .collect() 16 60 } 17 61 }
+3
years/2025/src/examples/day_2/1.txt
··· 1 + 11-22,95-115,998-1012,1188511880-1188511890,222220-222224, 2 + 1698522-1698528,446443-446449,38593856-38593862,565653-565659, 3 + 824824821-824824827,2121212118-2121212124
+3
years/2025/src/examples/day_2/2.txt
··· 1 + 11-22,95-115,998-1012,1188511880-1188511890,222220-222224, 2 + 1698522-1698528,446443-446449,38593856-38593862,565653-565659, 3 + 824824821-824824827,2121212118-2121212124