tangled
alpha
login
or
join now
nove.dev
/
aoc-2025
1
fork
atom
:)
1
fork
atom
overview
issues
pulls
pipelines
day5 part2
nove.dev
3 months ago
fe405dd4
1a27538d
+34
-8
2 changed files
expand all
collapse all
unified
split
src
day5.rs
lib.rs
+27
-1
src/day5.rs
···
10
}
11
12
pub fn day5_part2(input: &str) -> String {
13
-
todo!()
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
14
}
15
16
fn parse(input: &str) -> (Vec<RangeInclusive<i64>>, Vec<i64>) {
···
10
}
11
12
pub fn day5_part2(input: &str) -> String {
13
+
let (mut ranges, _) = parse(input);
14
+
ranges.sort_by_key(|r| *r.start());
15
+
let collapsed = collapse(&ranges);
16
+
let total_range = collapsed
17
+
.into_iter()
18
+
.fold(0, |acc, elem| acc + (elem.end() - elem.start() + 1));
19
+
total_range.to_string()
20
+
}
21
+
22
+
/// ranges must be sorted
23
+
fn collapse(ranges: &[RangeInclusive<i64>]) -> Vec<RangeInclusive<i64>> {
24
+
let mut collapsed = vec![];
25
+
26
+
let mut current_range = ranges[0].clone();
27
+
for range in ranges {
28
+
if range.start() <= current_range.end() {
29
+
if range.end() > current_range.end() {
30
+
current_range = *current_range.start()..=*range.end()
31
+
}
32
+
} else {
33
+
collapsed.push(current_range);
34
+
current_range = range.clone();
35
+
}
36
+
}
37
+
collapsed.push(current_range);
38
+
39
+
collapsed
40
}
41
42
fn parse(input: &str) -> (Vec<RangeInclusive<i64>>, Vec<i64>) {
+7
-7
src/lib.rs
···
131
let result = day5::day5_part1(include_str!("../input/day5.txt"));
132
assert_eq!(result, "885");
133
}
134
-
// #[test]
135
-
// fn day5_part2_test() {
136
-
// let test_result = day5::day5_part2(include_str!("../input/day5.test.txt"));
137
-
// assert_eq!(test_result, "43");
138
-
// // let result = day5::day5_part2(include_str!("../input/day5.txt"));
139
-
// // assert_eq!(result, "7922");
140
-
// }
141
142
#[test]
143
fn day4_part1_test() {
···
131
let result = day5::day5_part1(include_str!("../input/day5.txt"));
132
assert_eq!(result, "885");
133
}
134
+
#[test]
135
+
fn day5_part2_test() {
136
+
let test_result = day5::day5_part2(include_str!("../input/day5.test.txt"));
137
+
assert_eq!(test_result, "14");
138
+
let result = day5::day5_part2(include_str!("../input/day5.txt"));
139
+
assert_eq!(result, "348115621205535");
140
+
}
141
142
#[test]
143
fn day4_part1_test() {