blob: a4b7e38a7cb4e630787342ea983600d96721ad50 (
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
50
|
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 contains(&self) -> bool {
return (self.0.start <= self.1.start && self.0.end >= self.1.end)
|| (self.0.start >= self.1.start && self.0.end <= self.1.end);
}
}
fn main() {
let sum: usize = include_str!("../../example")
.lines()
.map(|l| Pair::new(l))
.map(|p| p.contains())
.filter(|r| *r)
.count();
println!("{}", sum);
}
|