-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.rs
66 lines (57 loc) · 1.55 KB
/
day04.rs
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use std::ops::RangeInclusive;
type Assignment = RangeInclusive<u32>;
struct AssignmentPair(Assignment, Assignment);
impl AssignmentPair {
fn new(s: &str) -> Self {
let pair: Vec<&str> = s.split(',').collect();
Self(
AssignmentPair::assignment_from_str(pair[0]),
AssignmentPair::assignment_from_str(pair[1]),
)
}
fn assignment_from_str(s: &str) -> Assignment {
let bounds: Vec<u32> = s.split('-').map(|i| i.parse::<u32>().unwrap()).collect();
Assignment::new(bounds[0], bounds[1])
}
fn has_superset(&self) -> bool {
self.0.start() <= self.1.start() && self.1.end() <= self.0.end()
|| self.1.start() <= self.0.start() && self.0.end() <= self.1.end()
}
fn has_overlap(&self) -> bool {
self.0.start() <= self.1.end() && self.1.start() <= self.0.end()
}
}
pub fn part1(input: &str) -> String {
let output: i32 = input
.lines()
.map(AssignmentPair::new)
.map(|a| if a.has_superset() { 1 } else { 0 })
.sum();
format!("{output}")
}
pub fn part2(input: &str) -> String {
let output: i32 = input
.lines()
.map(AssignmentPair::new)
.map(|a| if a.has_overlap() { 1 } else { 0 })
.sum();
format!("{output}")
}
#[cfg(test)]
mod tests {
use super::*;
const INPUT: &str = r#"2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8"#;
#[test]
fn test_part1() {
assert_eq!(part1(INPUT), "2");
}
#[test]
fn test_part2() {
assert_eq!(part2(INPUT), "4");
}
}