Skip to content

Commit dead762

Browse files
committed
chore: add y2025::day_02
1 parent 7973843 commit dead762

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
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.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ anyhow = "1.0.100"
88
aocf = "0.1.21"
99
bit-vec = "0.8.0"
1010
clap = { version = "4.5.53", features = ["derive"] }
11+
fancy-regex = "0.16.2"
1112
itertools = "0.14.0"
1213
num = "0.4.3"
1314
paste = "1.0.15"

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 }
Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,63 @@
1+
use std::ops::RangeInclusive;
2+
use std::str::FromStr;
3+
4+
use aoclp::num::Integer;
5+
use aoclp::solvers_impl::input::safe_get_input_as_one_vec;
6+
use fancy_regex::Regex;
7+
use itertools::Itertools;
8+
19
pub fn part_1() -> usize {
2-
0
10+
let re = Regex::new(r"^(\d+)\1$").unwrap();
11+
sum_of_invalid(&re)
312
}
413

514
pub fn part_2() -> usize {
6-
0
15+
let re = Regex::new(r"^(\d+)(\1)+$").unwrap();
16+
sum_of_invalid(&re)
17+
}
18+
19+
fn sum_of_invalid(re: &Regex) -> usize {
20+
input()
21+
.into_iter()
22+
.flat_map(|range| range.into_iter())
23+
.filter(|id| re.is_match(&id.to_string()).unwrap())
24+
.sum()
25+
}
26+
27+
fn num_digits(n: usize) -> usize {
28+
((n as f64).log10() + 1.0).floor() as usize // hax
29+
}
30+
31+
fn invalid(id: usize) -> bool {
32+
let num_digits = num_digits(id);
33+
if num_digits.is_even() {
34+
let midpoint = 10usize.pow(num_digits as u32 / 2);
35+
return (id / midpoint) == (id % midpoint);
36+
}
37+
38+
false
39+
}
40+
41+
struct IdRange(RangeInclusive<usize>);
42+
43+
impl IntoIterator for IdRange {
44+
type Item = <RangeInclusive<usize> as IntoIterator>::Item;
45+
type IntoIter = <RangeInclusive<usize> as IntoIterator>::IntoIter;
46+
47+
fn into_iter(self) -> Self::IntoIter {
48+
self.0
49+
}
50+
}
51+
52+
impl FromStr for IdRange {
53+
type Err = aoclp::Error;
54+
55+
fn from_str(s: &str) -> Result<Self, Self::Err> {
56+
let (from, to) = s.split('-').collect_tuple().unwrap();
57+
Ok(Self(from.parse()?..=to.parse()?))
58+
}
59+
}
60+
61+
fn input() -> Vec<IdRange> {
62+
safe_get_input_as_one_vec(2025, 2)
763
}

0 commit comments

Comments
 (0)