Skip to content

Commit 5af9349

Browse files
committed
feat(05/2025): solve first part
1 parent ad469bf commit 5af9349

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
| [Day 2: Gift Shop](src/solutions/year2025/day02.rs) | ⭐⭐ | 65.103 | 81.719 |
1616
| [Day 3: Lobby](src/solutions/year2025/day03.rs) | ⭐⭐ | 0.131 | 0.133 |
1717
| [Day 4: Printing Department](src/solutions/year2025/day04.rs) | ⭐⭐ | 3.615 | 9.998 |
18-
| [Day 5: Cafeteria](src/solutions/year2025/day05.rs) | | - | - |
18+
| [Day 5: Cafeteria](src/solutions/year2025/day05.rs) | | 0.275 | - |
1919

2020
# 2024
2121

src/solutions/year2025/day05.rs

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,68 @@
11
use crate::solutions::Solution;
2+
use itertools::Itertools;
3+
use std::ops::RangeInclusive;
24

35
pub struct Day05;
46

57
impl Solution for Day05 {
6-
fn part_one(&self, _input: &str) -> String {
7-
String::from("0")
8+
fn part_one(&self, input: &str) -> String {
9+
let (ranges, ids) = self.parse(input);
10+
11+
ids.iter()
12+
.filter(|id| ranges.iter().any(|range| range.contains(id)))
13+
.count()
14+
.to_string()
815
}
916

1017
fn part_two(&self, _input: &str) -> String {
1118
String::from("0")
1219
}
1320
}
1421

22+
impl Day05 {
23+
fn parse(&self, input: &str) -> (Vec<RangeInclusive<usize>>, Vec<usize>) {
24+
let (ranges_str, ids_str) = input.split_once("\n\n").unwrap();
25+
26+
let ranges = ranges_str
27+
.lines()
28+
.map(|line| {
29+
let (start, end) = line.split_once("-").unwrap();
30+
31+
let start = start.parse::<usize>().unwrap();
32+
let end = end.parse::<usize>().unwrap();
33+
34+
start..=end
35+
})
36+
.collect_vec();
37+
38+
let ids = ids_str
39+
.lines()
40+
.map(|line| line.parse::<usize>().unwrap())
41+
.collect_vec();
42+
43+
(ranges, ids)
44+
}
45+
}
46+
1547
#[cfg(test)]
1648
mod tests {
1749
use crate::solutions::year2025::day05::Day05;
1850
use crate::solutions::Solution;
1951

20-
const EXAMPLE: &str = r#""#;
52+
const EXAMPLE: &str = r#"3-5
53+
10-14
54+
16-20
55+
12-18
56+
57+
1
58+
5
59+
8
60+
11
61+
17
62+
32"#;
2163

2264
#[test]
2365
fn part_one_example_test() {
24-
assert_eq!("0", Day05.part_one(EXAMPLE));
66+
assert_eq!("3", Day05.part_one(EXAMPLE));
2567
}
2668
}

0 commit comments

Comments
 (0)