blob: d7f378a880e717d99e54bded2094cdae9250be39 (
plain)
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
|
use itertools::Itertools;
#[derive(Debug)]
struct Elf {
start: usize,
end: usize,
}
#[derive(Debug)]
struct Pair(Elf, Elf);
impl Elf {
pub fn new(range: &str) -> Self {
if let Some((start, end)) = range.to_string().split("-").collect_tuple() {
let start: usize = start.parse().unwrap();
let end: usize = end.parse().unwrap();
Self { start, end }
} else {
panic!("Expected two elements");
}
}
}
impl Pair {
pub fn new(line: &str) -> Self {
if let Some((first, second)) = line.to_string().split(",").collect_tuple() {
Pair(Elf::new(first), Elf::new(second))
} else {
panic!("Expected two elements");
}
}
}
impl Pair {
fn overlaps(&self) -> bool {
!(self.0.end < self.1.start || self.1.end < self.0.start)
}
}
fn main() {
let sum: usize = include_str!("../../input")
.lines()
.map(|l| Pair::new(l))
.map(|p| p.overlaps())
.filter(|r| *r)
.count();
println!("{}", sum);
}
|