Day 02 - Gift Shop
Language: Rust
Problem https://adventofcode.com/2025/day/2
Part 1:
The input is a single comma-separated line of inclusive ranges like 11-22. The task is to
find all invalid product IDs across those ranges and sum them. An ID is invalid if it’s some
digit sequence repeated exactly twice, like 6464 (64 twice) or 123123 (123 twice).
I split on commas, parse each start-end, then iterate the full range and filter by the
predicate:
let (a, b) = range.split_once('-').unwrap();
let start: u64 = a.parse().unwrap();
let end: u64 = b.parse().unwrap();
To check if a number is of the form XYXY, the digit count has to be even and the two halves
have to match:
if s.len() % 2 != 0 { return false; }
let (l, r) = s.split_at(s.len() / 2);
l == r
Part 2:
Now a number is invalid if it’s any block repeated two or more times, not just exactly twice.
So 121212 (12 three times) also counts.
I try every possible block length up to len/2, skip lengths that don’t divide evenly, then
check that every chunk matches the first:
for block_len in 1..=(len / 2) {
if len % block_len != 0 { continue; }
let block = &s[..block_len];
for i in (block_len..len).step_by(block_len) {
if &s[i..i + block_len] != block { ok = false; break; }
}
if ok { return true; }
}
Both parts follow the same shape overall, only the predicate changes:
(start..=end)
.filter(|&n| is_repeated_at_least_twice(n))
.sum::<u64>()
Solution: https://github.com/Elyrial/AdventOfCode/blob/main/src/solutions/year2025/day02.rs
No C writeup yet.