Day 4, puzzle 2

This commit is contained in:
jazzpi 2022-12-13 15:38:10 +01:00
parent b014865ff2
commit d1a30ea629
2 changed files with 13 additions and 0 deletions

9
src/bin/d4p2.rs Normal file
View File

@ -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);
}

View File

@ -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)> {