Skip to content

Commit eeab266

Browse files
committed
feat(2025): refactor day 2
Refactored Day 2 of 2025 to use the common framework. Successfully migrated solution and integrated it into the runner.
1 parent ec9aad2 commit eeab266

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

2025/inputs/2/prod.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
9595822750-9596086139,1957-2424,88663-137581,48152-65638,12354817-12385558,435647-489419,518494-609540,2459-3699,646671-688518,195-245,295420-352048,346-514,8686839668-8686892985,51798991-51835611,8766267-8977105,2-17,967351-995831,6184891-6331321,6161577722-6161678622,912862710-913019953,6550936-6625232,4767634976-4767662856,2122995-2257010,1194-1754,779-1160,22-38,4961-6948,39-53,102-120,169741-245433,92902394-92956787,531-721,64-101,15596-20965,774184-943987,8395-11781,30178-47948,94338815-94398813

2025/rust/src/day02.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use aoc_rust_common::Solution;
2+
use std::fmt::Display;
3+
use std::str::FromStr;
4+
5+
pub struct Day02;
6+
7+
type Id = u64;
8+
9+
#[derive(Debug, Clone, Copy)]
10+
struct Range { lower: Id, upper: Id }
11+
12+
impl FromStr for Range {
13+
type Err = ();
14+
fn from_str(s: &str) -> Result<Self, Self::Err> {
15+
let parts: Vec<Id> = s.split('-').map(|p| p.parse().unwrap()).collect();
16+
Ok(Range { lower: parts[0], upper: parts[1] })
17+
}
18+
}
19+
20+
fn is_double(n: Id) -> bool {
21+
let s = n.to_string();
22+
let len = s.len();
23+
len % 2 == 0 && s[..len/2] == s[len/2..]
24+
}
25+
26+
fn is_at_least_double(n: Id) -> bool {
27+
let s = n.to_string();
28+
let len = s.len();
29+
(1..len).filter(|d| len % d == 0 && len / d >= 2).any(|d| {
30+
let pattern = &s[..d];
31+
s.as_bytes().chunks(d).all(|c| c == pattern.as_bytes())
32+
})
33+
}
34+
35+
impl Solution for Day02 {
36+
fn year(&self) -> u32 { 2025 }
37+
fn day(&self) -> u32 { 2 }
38+
39+
fn part1(&self, input: &str) -> Box<dyn Display> {
40+
let ranges: Vec<Range> = input.trim().split(',').map(|s| s.parse().unwrap()).collect();
41+
let sum: Id = ranges.iter().flat_map(|r| (r.lower..=r.upper).filter(|&n| is_double(n))).sum();
42+
Box::new(sum)
43+
}
44+
45+
fn part2(&self, input: &str) -> Box<dyn Display> {
46+
let ranges: Vec<Range> = input.trim().split(',').map(|s| s.parse().unwrap()).collect();
47+
let sum: Id = ranges.iter().flat_map(|r| (r.lower..=r.upper).filter(|&n| is_at_least_double(n))).sum();
48+
Box::new(sum)
49+
}
50+
}

2025/rust/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
mod day01;
2+
mod day02;
23
use aoc_rust_common::run_solution;
34
use day01::Day01;
5+
use day02::Day02;
46

57
fn main() {
68
run_solution(Day01);
9+
run_solution(Day02);
710
}

0 commit comments

Comments
 (0)