Skip to content

Commit 9ad9e2f

Browse files
committed
chore: support both fast and slow approaches for y2025::day_02
1 parent e0fe991 commit 9ad9e2f

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

Cargo.lock

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aoclp_solutions/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ rust-version = "1.81.0"
88
aoclp = { path = "../aoclp" }
99
bit-vec = { workspace = true }
1010
clap = { workspace = true, features = ["derive"] }
11+
fancy-regex = { workspace = true }
1112
itertools = { workspace = true }
1213
paste = { workspace = true }
1314
primes = { workspace = true }

aoclp_solutions/src/y2025/day_02.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,43 @@ use std::str::FromStr;
33

44
use aoclp::num::Integer;
55
use aoclp::solvers_impl::input::safe_get_input_as_one_vec;
6+
use fancy_regex::Regex;
67
use itertools::Itertools;
78

89
pub fn part_1() -> usize {
10+
match be_fast() {
11+
true => sum_fast(invalid),
12+
false => sum_slow(Regex::new(r"^(\d+)\1$").unwrap()),
13+
}
14+
}
15+
16+
pub fn part_2() -> usize {
17+
match be_fast() {
18+
true => sum_fast(invalid),
19+
false => sum_slow(Regex::new(r"^(\d+)\1+$").unwrap()),
20+
}
21+
}
22+
23+
fn be_fast() -> bool {
24+
true
25+
}
26+
27+
fn sum_fast<P>(mut pred: P) -> usize
28+
where
29+
P: FnMut(usize) -> bool,
30+
{
931
input()
1032
.into_iter()
1133
.flat_map(|range| range.into_iter())
12-
.filter(|id| invalid(*id))
34+
.filter(|id| pred(*id))
1335
.sum()
1436
}
1537

16-
pub fn part_2() -> usize {
38+
fn sum_slow(re: Regex) -> usize {
1739
input()
1840
.into_iter()
1941
.flat_map(|range| range.into_iter())
20-
.filter(|id| invalid_fancy(*id))
42+
.filter(|id| re.is_match(&id.to_string()).unwrap())
2143
.sum()
2244
}
2345

0 commit comments

Comments
 (0)