tangled
alpha
login
or
join now
bwc9876.dev
/
advent
0
fork
atom
Advent of Code solutions
0
fork
atom
overview
issues
pulls
pipelines
Day 2
bwc9876.dev
3 months ago
36e9353c
9eaca333
verified
This commit was signed with the committer's
known signature
.
bwc9876.dev
SSH Key Fingerprint:
SHA256:DanMEP/RNlSC7pAVbnXO6wzQV00rqyKj053tz4uH5gQ=
+57
-5
5 changed files
expand all
collapse all
unified
split
Cargo.lock
years
2025
Cargo.toml
src
day_2.rs
examples
day_2
1.txt
2.txt
+1
Cargo.lock
···
338
338
dependencies = [
339
339
"advent_core",
340
340
"macros",
341
341
+
"rayon",
341
342
"utils",
342
343
]
+1
years/2025/Cargo.toml
···
7
7
[dependencies]
8
8
advent_core = { path = "../../advent_core" }
9
9
macros = { path = "../../macros" }
10
10
+
rayon = "1.11.0"
10
11
utils = { path = "../../utils" }
+49
-5
years/2025/src/day_2.rs
···
1
1
+
use std::ops::RangeInclusive;
1
2
2
3
use advent_core::{Day, day_stuff, ex_for_day};
4
4
+
use utils::num::{num_digits, split_num_at, split_num_once};
3
5
4
6
pub struct Day2;
5
7
6
8
impl Day for Day2 {
9
9
+
day_stuff!(2, "1227775554", "4174379265", Vec<RangeInclusive<usize>>);
7
10
8
8
-
day_stuff!(2, "", "");
11
11
+
fn part_1(input: Self::Input) -> Option<String> {
12
12
+
let ans = input
13
13
+
.into_iter()
14
14
+
.flat_map(|r| {
15
15
+
r.into_iter().filter(|x| {
16
16
+
let (l, r) = split_num_once(*x);
17
17
+
l == r
18
18
+
})
19
19
+
})
20
20
+
.sum::<usize>();
9
21
10
10
-
fn part_1(_input: Self::Input) -> Option<String> {
11
11
-
None
22
22
+
Some(ans.to_string())
12
23
}
13
24
14
14
-
fn part_2(_input: Self::Input) -> Option<String> {
15
15
-
None
25
25
+
fn part_2(input: Self::Input) -> Option<String> {
26
26
+
let ans = input
27
27
+
.into_iter()
28
28
+
.flat_map(|r| {
29
29
+
r.into_iter().filter(|x| {
30
30
+
let digs = num_digits(*x);
31
31
+
(2..=digs).into_iter().filter(|n| digs % n == 0).any(|n| {
32
32
+
let mut parts = Vec::with_capacity(n);
33
33
+
let mut current = *x;
34
34
+
let split_at = digs / n;
35
35
+
for _ in 0..n {
36
36
+
let (rest, part) = split_num_at(current, split_at as u32);
37
37
+
parts.push(part);
38
38
+
current = rest;
39
39
+
}
40
40
+
parts
41
41
+
.first()
42
42
+
.copied()
43
43
+
.map_or(false, |first| parts.into_iter().all(|x| x == first))
44
44
+
})
45
45
+
})
46
46
+
})
47
47
+
.sum::<usize>();
48
48
+
49
49
+
Some(ans.to_string())
50
50
+
}
51
51
+
52
52
+
fn parse_input(input: &str) -> Self::Input {
53
53
+
input
54
54
+
.split(',')
55
55
+
.map(|r| {
56
56
+
let (l, r) = r.split_once('-').unwrap();
57
57
+
(l.trim().parse::<usize>().unwrap())..=(r.trim().parse::<usize>().unwrap())
58
58
+
})
59
59
+
.collect()
16
60
}
17
61
}
+3
years/2025/src/examples/day_2/1.txt
···
1
1
+
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,
2
2
+
1698522-1698528,446443-446449,38593856-38593862,565653-565659,
3
3
+
824824821-824824827,2121212118-2121212124
+3
years/2025/src/examples/day_2/2.txt
···
1
1
+
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,
2
2
+
1698522-1698528,446443-446449,38593856-38593862,565653-565659,
3
3
+
824824821-824824827,2121212118-2121212124