summaryrefslogtreecommitdiffstats
path: root/2022/day4/part2/src
diff options
context:
space:
mode:
authorYigit Sever2022-12-06 14:47:43 +0300
committerYigit Sever2022-12-06 14:47:43 +0300
commit244d3dfa233371db0adcff329283bb1ce9bba9f5 (patch)
treed6ec2d6682a45c823bf0ee9f921f861f3cefe118 /2022/day4/part2/src
parentef7e8df2825480f34c1034015a8221c09f6ebdf6 (diff)
downloadaoc-244d3dfa233371db0adcff329283bb1ce9bba9f5.tar.gz
aoc-244d3dfa233371db0adcff329283bb1ce9bba9f5.tar.bz2
aoc-244d3dfa233371db0adcff329283bb1ce9bba9f5.zip
2022, day4: done
Diffstat (limited to '2022/day4/part2/src')
-rw-r--r--2022/day4/part2/src/main.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/2022/day4/part2/src/main.rs b/2022/day4/part2/src/main.rs
new file mode 100644
index 0000000..d7f378a
--- /dev/null
+++ b/2022/day4/part2/src/main.rs
@@ -0,0 +1,49 @@
1use itertools::Itertools;
2
3#[derive(Debug)]
4struct Elf {
5 start: usize,
6 end: usize,
7}
8
9#[derive(Debug)]
10struct Pair(Elf, Elf);
11
12impl Elf {
13 pub fn new(range: &str) -> Self {
14 if let Some((start, end)) = range.to_string().split("-").collect_tuple() {
15 let start: usize = start.parse().unwrap();
16 let end: usize = end.parse().unwrap();
17 Self { start, end }
18 } else {
19 panic!("Expected two elements");
20 }
21 }
22}
23
24impl Pair {
25 pub fn new(line: &str) -> Self {
26 if let Some((first, second)) = line.to_string().split(",").collect_tuple() {
27 Pair(Elf::new(first), Elf::new(second))
28 } else {
29 panic!("Expected two elements");
30 }
31 }
32}
33
34impl Pair {
35 fn overlaps(&self) -> bool {
36 !(self.0.end < self.1.start || self.1.end < self.0.start)
37 }
38}
39
40fn main() {
41 let sum: usize = include_str!("../../input")
42 .lines()
43 .map(|l| Pair::new(l))
44 .map(|p| p.overlaps())
45 .filter(|r| *r)
46 .count();
47
48 println!("{}", sum);
49}