|
| 1 | +#![allow(non_snake_case)] |
| 2 | + |
| 3 | +use aoc::Puzzle; |
| 4 | +use std::ops::RangeInclusive; |
| 5 | + |
| 6 | +enum Mode { |
| 7 | + ExactlyOnce, |
| 8 | + AtLeastOnce, |
| 9 | +} |
| 10 | + |
| 11 | +impl Mode { |
| 12 | + fn check(&self, n: u64) -> bool { |
| 13 | + fn sub_check(s: &str, sub_len: usize) -> bool { |
| 14 | + let sz = s.len(); |
| 15 | + if !sz.is_multiple_of(sub_len) { |
| 16 | + return false; |
| 17 | + } |
| 18 | + let sub = &s[..sub_len]; |
| 19 | + for i in (sub_len..sz).step_by(sub_len) { |
| 20 | + if !s[i..].starts_with(sub) { |
| 21 | + return false; |
| 22 | + } |
| 23 | + } |
| 24 | + true |
| 25 | + } |
| 26 | + |
| 27 | + let s = n.to_string(); |
| 28 | + match self { |
| 29 | + Mode::ExactlyOnce => { |
| 30 | + s.len().is_multiple_of(2) && sub_check(&s, s.len() / 2) |
| 31 | + } |
| 32 | + Mode::AtLeastOnce => { |
| 33 | + for i in 1..=s.len().div_euclid(2) { |
| 34 | + if sub_check(&s, i) { |
| 35 | + return true; |
| 36 | + } |
| 37 | + } |
| 38 | + false |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +struct AoC2025_02; |
| 45 | + |
| 46 | +impl AoC2025_02 { |
| 47 | + fn solve(&self, ranges: &[RangeInclusive<u64>], mode: Mode) -> u64 { |
| 48 | + ranges |
| 49 | + .iter() |
| 50 | + .flat_map(|rng| rng.clone().step_by(1)) |
| 51 | + .filter(|n| mode.check(*n)) |
| 52 | + .sum() |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl aoc::Puzzle for AoC2025_02 { |
| 57 | + type Input = Vec<RangeInclusive<u64>>; |
| 58 | + type Output1 = u64; |
| 59 | + type Output2 = u64; |
| 60 | + |
| 61 | + aoc::puzzle_year_day!(2025, 2); |
| 62 | + |
| 63 | + fn parse_input(&self, lines: Vec<String>) -> Self::Input { |
| 64 | + lines[0] |
| 65 | + .split(',') |
| 66 | + .map(|sp| { |
| 67 | + let (lo, hi) = sp.split_once('-').unwrap(); |
| 68 | + RangeInclusive::new( |
| 69 | + lo.parse::<u64>().unwrap(), |
| 70 | + hi.parse::<u64>().unwrap(), |
| 71 | + ) |
| 72 | + }) |
| 73 | + .collect::<Vec<_>>() |
| 74 | + } |
| 75 | + |
| 76 | + fn part_1(&self, ranges: &Self::Input) -> Self::Output1 { |
| 77 | + self.solve(ranges, Mode::ExactlyOnce) |
| 78 | + } |
| 79 | + |
| 80 | + fn part_2(&self, ranges: &Self::Input) -> Self::Output2 { |
| 81 | + self.solve(ranges, Mode::AtLeastOnce) |
| 82 | + } |
| 83 | + |
| 84 | + fn samples(&self) { |
| 85 | + aoc::puzzle_samples! { |
| 86 | + self, part_1, TEST, 1227775554_u64, |
| 87 | + self, part_2, TEST, 4174379265_u64 |
| 88 | + }; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +fn main() { |
| 93 | + AoC2025_02 {}.run(std::env::args()); |
| 94 | +} |
| 95 | + |
| 96 | +const TEST: &str = "11-22,95-115,998-1012,1188511880-1188511890,222220-222224,\ |
| 97 | + 1698522-1698528,446443-446449,38593856-38593862,\ |
| 98 | + 565653-565659,824824821-824824827,2121212118-2121212124"; |
| 99 | + |
| 100 | +#[cfg(test)] |
| 101 | +mod tests { |
| 102 | + use super::*; |
| 103 | + |
| 104 | + #[test] |
| 105 | + pub fn samples() { |
| 106 | + AoC2025_02 {}.samples(); |
| 107 | + } |
| 108 | +} |
0 commit comments