diff --git a/Cargo.lock b/Cargo.lock index d801ed1..57c7f83 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -338,5 +338,6 @@ version = "0.1.0" dependencies = [ "advent_core", "macros", + "rayon", "utils", ] diff --git a/years/2025/Cargo.toml b/years/2025/Cargo.toml index 6fb50c4..e025283 100644 --- a/years/2025/Cargo.toml +++ b/years/2025/Cargo.toml @@ -7,4 +7,5 @@ edition = "2024" [dependencies] advent_core = { path = "../../advent_core" } macros = { path = "../../macros" } +rayon = "1.11.0" utils = { path = "../../utils" } diff --git a/years/2025/src/day_2.rs b/years/2025/src/day_2.rs index 00430bc..3e861c2 100644 --- a/years/2025/src/day_2.rs +++ b/years/2025/src/day_2.rs @@ -1,17 +1,61 @@ +use std::ops::RangeInclusive; use advent_core::{Day, day_stuff, ex_for_day}; +use utils::num::{num_digits, split_num_at, split_num_once}; pub struct Day2; impl Day for Day2 { + day_stuff!(2, "1227775554", "4174379265", Vec>); - day_stuff!(2, "", ""); + fn part_1(input: Self::Input) -> Option { + let ans = input + .into_iter() + .flat_map(|r| { + r.into_iter().filter(|x| { + let (l, r) = split_num_once(*x); + l == r + }) + }) + .sum::(); - fn part_1(_input: Self::Input) -> Option { - None + Some(ans.to_string()) } - fn part_2(_input: Self::Input) -> Option { - None + fn part_2(input: Self::Input) -> Option { + let ans = input + .into_iter() + .flat_map(|r| { + r.into_iter().filter(|x| { + let digs = num_digits(*x); + (2..=digs).into_iter().filter(|n| digs % n == 0).any(|n| { + let mut parts = Vec::with_capacity(n); + let mut current = *x; + let split_at = digs / n; + for _ in 0..n { + let (rest, part) = split_num_at(current, split_at as u32); + parts.push(part); + current = rest; + } + parts + .first() + .copied() + .map_or(false, |first| parts.into_iter().all(|x| x == first)) + }) + }) + }) + .sum::(); + + Some(ans.to_string()) + } + + fn parse_input(input: &str) -> Self::Input { + input + .split(',') + .map(|r| { + let (l, r) = r.split_once('-').unwrap(); + (l.trim().parse::().unwrap())..=(r.trim().parse::().unwrap()) + }) + .collect() } } diff --git a/years/2025/src/examples/day_2/1.txt b/years/2025/src/examples/day_2/1.txt index e69de29..b5e6998 100644 --- a/years/2025/src/examples/day_2/1.txt +++ b/years/2025/src/examples/day_2/1.txt @@ -0,0 +1,3 @@ +11-22,95-115,998-1012,1188511880-1188511890,222220-222224, +1698522-1698528,446443-446449,38593856-38593862,565653-565659, +824824821-824824827,2121212118-2121212124 diff --git a/years/2025/src/examples/day_2/2.txt b/years/2025/src/examples/day_2/2.txt index e69de29..b5e6998 100644 --- a/years/2025/src/examples/day_2/2.txt +++ b/years/2025/src/examples/day_2/2.txt @@ -0,0 +1,3 @@ +11-22,95-115,998-1012,1188511880-1188511890,222220-222224, +1698522-1698528,446443-446449,38593856-38593862,565653-565659, +824824821-824824827,2121212118-2121212124