-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday05.rs
More file actions
51 lines (38 loc) · 1.21 KB
/
Copy pathday05.rs
File metadata and controls
51 lines (38 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! # Cafeteria
//!
//!
use crate::common::parse::ParseInteger;
type InputParsed<'a> = (Vec<(u64, u64)>, &'a str);
pub fn parse<'a>(input: &'a str) -> InputParsed<'a> {
let (top, bottom) = input.trim().split_once("\n\n").unwrap();
let ranges = top
.lines()
.map(|l| {
let (s, e) = l.split_once('-').unwrap();
(s.parse::<u64>().unwrap(), e.parse::<u64>().unwrap())
})
.collect();
(ranges, bottom)
}
pub fn part1(input: &InputParsed) -> usize {
let (ranges, bottom_str) = input;
let ingredients = bottom_str.parse_uint_iter::<u64>();
ingredients.filter(|&ing| ranges.iter().any(|&(start, end)| ing >= start && ing <= end)).count()
}
pub fn part2(input: &InputParsed) -> u64 {
let mut ranges = input.0.clone();
ranges.sort_unstable();
let last = ranges[0];
let (result, last) = ranges.iter().skip(1).fold((0, last), |acc, &range| {
let mut result = acc.0;
let mut last = acc.1;
if last.1 < range.0 {
result += last.1 - last.0 + 1;
last = range;
} else {
last.1 = last.1.max(range.1);
}
(result, last)
});
result + last.1 - last.0 + 1
}