diff --git a/src/bin/day04.rs b/src/bin/day04.rs index 4910c16..6bd8ddb 100644 --- a/src/bin/day04.rs +++ b/src/bin/day04.rs @@ -1,6 +1,5 @@ -use std::ops::{Range, RangeInclusive}; +use std::ops::RangeInclusive; use std::str::FromStr; -use std::{collections::HashSet, ops::RangeBounds}; use aoc_2022::*; @@ -33,29 +32,31 @@ impl FromStr for Assignment { } } +impl Assignment { + fn fully_overlap(&self) -> bool { + let Assignment(l, r) = self; + + (l.start() <= r.start() && r.end() <= l.end()) + || (r.start() <= l.start() && l.end() <= r.end()) + } + + fn overlap(&self) -> bool { + let Assignment(l, r) = self; + + (l.contains(r.start()) || l.contains(r.end())) + || (r.contains(l.start()) || r.contains(l.end())) + } +} + type Input = Vec; -type Output = i32; +type Output = usize; fn part_1(input: &Input) -> Output { - input - .iter() - .filter(|Assignment(l, r)| { - (l.start() <= r.start() && r.end() <= l.end()) - || (r.start() <= l.start() && l.end() <= r.end()) - }) - .count() as i32 + input.iter().filter(|a| a.fully_overlap()).count() } fn part_2(input: &Input) -> Output { - input - .iter() - .filter(|Assignment(l, r)| { - l.contains(r.start()) - || l.contains(r.end()) - || r.contains(l.start()) - || r.contains(l.end()) - }) - .count() as i32 + input.iter().filter(|a| a.overlap()).count() } fn parse_input(pathname: &str) -> Input {