feat: Day 2

This commit is contained in:
Jan-Bulthuis 2025-12-02 07:27:01 +01:00
parent 91d9d7b1dc
commit 3740d93f81
2 changed files with 87 additions and 1 deletions

86
aoc_2025/src/day2.rs Normal file
View File

@ -0,0 +1,86 @@
use crate::AoC;
use winnow::{
Parser, Result,
ascii::digit1,
combinator::{separated, separated_pair},
};
type Parsed = Vec<(u64, u64)>;
const AOC: AoC<Parsed> = AoC::new(parse, part1, part2);
fn parse(input: String) -> Parsed {
let mut input: &str = input.as_str();
separated(1.., parse_range, ",")
.parse_next(&mut input)
.unwrap()
}
fn parse_range(input: &mut &str) -> Result<(u64, u64)> {
separated_pair(digit1.parse_to(), "-", digit1.parse_to()).parse_next(input)
}
fn part1(input: &Parsed) -> String {
let res = input
.iter()
.map(|(from, to)| {
(*from..=*to)
.filter(|i| {
let n = (i.ilog10() + 1) >> 1;
let p = 10u64.pow(n);
i / p == i % p
})
.sum::<u64>()
})
.sum::<u64>();
format!("{res}")
}
fn part2(input: &Parsed) -> String {
let res = input
.iter()
.map(|(from, to)| {
(*from..=*to)
.filter(|i| {
let n = (i.ilog10() + 1);
(1..=(n >> 1)).filter(|p| n % p == 0).any(|p| {
(1..(n / p))
.all(|s| i % 10u64.pow(p) == (i / 10u64.pow(p * s)) % 10u64.pow(p))
})
})
.sum::<u64>()
})
.sum::<u64>();
format!("{res}")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn part1_test() {
AOC.assert1(
"11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124",
"1227775554",
);
}
#[test]
fn part1_solve() {
AOC.assert1(include_str!("../input/day2.txt"), "12599655151");
}
#[test]
fn part2_test() {
AOC.assert2(
"11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124",
"4174379265",
);
}
#[test]
fn part2_solve() {
AOC.assert2(include_str!("../input/day2.txt"), "20942028255");
}
}

View File

@ -1,7 +1,7 @@
#![allow(dead_code)]
#![allow(unused)]
mod day1;
mod day2;
struct AoC<T> {
parse: fn(String) -> T,