Skip to content

Commit 9e23445

Browse files
committed
2025 Day 5
1 parent f77e97c commit 9e23445

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/bin/2025_05/main.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use aoc::{
2+
common, io,
3+
range::{Range, RangeUnion},
4+
};
5+
6+
fn solve<const PART: usize>(input: &str) -> usize {
7+
let mut ans = 0;
8+
let mut ru: RangeUnion<usize> = RangeUnion::new();
9+
for line in input.lines() {
10+
if line.is_empty() {
11+
continue;
12+
}
13+
if line.contains("-") {
14+
let tokens = io::tokenize_nums::<usize>(line, "-");
15+
ru.add_range(Range::new(tokens[0], tokens[1] + 1));
16+
} else {
17+
let token = io::parse_num::<usize>(line);
18+
if ru.contains(token) {
19+
ans += 1;
20+
}
21+
}
22+
}
23+
24+
if PART == 1 {
25+
ans
26+
} else {
27+
ru.spread()
28+
}
29+
}
30+
31+
fn main() {
32+
if let Some(input) = common::get_input() {
33+
common::timed(&input, solve::<1>, true);
34+
common::timed(&input, solve::<2>, false);
35+
}
36+
}
37+
38+
#[cfg(test)]
39+
mod tests {
40+
use super::*;
41+
42+
#[test]
43+
fn test_samples() {
44+
let sample_input = "3-5\n10-14\n16-20\n12-18\n\n1\n5\n8\n11\n17\n32";
45+
assert_eq!(solve::<1>(sample_input), 3);
46+
assert_eq!(solve::<2>(sample_input), 14);
47+
}
48+
}

0 commit comments

Comments
 (0)