Skip to content

Commit 9add9a9

Browse files
committed
2025 day 2
1 parent 2710d46 commit 9add9a9

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

crates/year2025/src/day02.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use utils::prelude::*;
2+
3+
/// Finding numbers with repeated digit patterns within ranges.
4+
#[derive(Clone, Debug)]
5+
pub struct Day02 {
6+
input: Vec<[u64; 2]>,
7+
}
8+
9+
impl Day02 {
10+
pub fn new(input: &str, _: InputType) -> Result<Self, InputError> {
11+
Ok(Self {
12+
input: parser::u64()
13+
.repeat_n(b'-')
14+
.repeat(b',', 1)
15+
.parse_complete(input)?,
16+
})
17+
}
18+
19+
#[must_use]
20+
pub fn part1(&self) -> u64 {
21+
let mut total = 0;
22+
for &[start, end] in &self.input {
23+
'num_loop: for i in start..=end {
24+
let num_digits = i.ilog10() + 1;
25+
26+
if num_digits % 2 != 0 {
27+
continue;
28+
}
29+
30+
let pattern_digits = num_digits / 2;
31+
let divisor = 10u64.pow(pattern_digits);
32+
let pattern = i % divisor;
33+
34+
let mut remaining = i;
35+
while remaining > 0 {
36+
let chunk = remaining % divisor;
37+
if chunk != pattern {
38+
continue 'num_loop;
39+
}
40+
remaining /= divisor;
41+
}
42+
43+
total += i;
44+
}
45+
}
46+
total
47+
}
48+
49+
#[must_use]
50+
pub fn part2(&self) -> u64 {
51+
let mut total = 0;
52+
for &[start, end] in &self.input {
53+
for i in start..=end {
54+
let num_digits = i.ilog10() + 1;
55+
56+
'pattern_loop: for pattern_digits in 1..=num_digits / 2 {
57+
if num_digits % pattern_digits != 0 {
58+
continue;
59+
}
60+
let divisor = 10u64.pow(pattern_digits);
61+
let pattern = i % divisor;
62+
63+
let mut remaining = i;
64+
while remaining > 0 {
65+
let chunk = remaining % divisor;
66+
if chunk != pattern {
67+
continue 'pattern_loop;
68+
}
69+
remaining /= divisor;
70+
}
71+
72+
total += i;
73+
break;
74+
}
75+
}
76+
}
77+
total
78+
}
79+
}
80+
81+
examples!(Day02 -> (u64, u64) [
82+
{
83+
input: "11-22,95-115,998-1012,1188511880-1188511890,222220-222224,\
84+
1698522-1698528,446443-446449,38593856-38593862,565653-565659,\
85+
824824821-824824827,2121212118-2121212124",
86+
part1: 1227775554,
87+
part2: 4174379265
88+
},
89+
]);

crates/year2025/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33

44
utils::year!(2025 => year2025, ${
55
1 => day01::Day01,
6+
2 => day02::Day02,
67
});

0 commit comments

Comments
 (0)