From d1a30ea6293a12e607ba7319a1f1558378c21d77 Mon Sep 17 00:00:00 2001 From: jazzpi Date: Tue, 13 Dec 2022 15:38:10 +0100 Subject: [PATCH] Day 4, puzzle 2 --- src/bin/d4p2.rs | 9 +++++++++ src/day4.rs | 4 ++++ 2 files changed, 13 insertions(+) create mode 100644 src/bin/d4p2.rs diff --git a/src/bin/d4p2.rs b/src/bin/d4p2.rs new file mode 100644 index 0000000..239ca70 --- /dev/null +++ b/src/bin/d4p2.rs @@ -0,0 +1,9 @@ +use aoc22::{day4, util}; + +pub fn main() { + let assignments = day4::parse_assignments(&util::parse_input()); + + let overlapping = assignments.iter().filter(|a| a.0.overlaps(&a.1)).count(); + + println!("{} assignments overlap", overlapping); +} diff --git a/src/day4.rs b/src/day4.rs index c257aaa..556230b 100644 --- a/src/day4.rs +++ b/src/day4.rs @@ -10,6 +10,10 @@ impl Assignment { pub fn contains(&self, other: &Self) -> bool { self.lower_bound <= other.lower_bound && self.upper_bound >= other.upper_bound } + + pub fn overlaps(&self, other: &Self) -> bool { + self.lower_bound <= other.upper_bound && self.upper_bound >= other.lower_bound + } } pub fn parse_assignments(input: &String) -> Vec<(Assignment, Assignment)> {