day(04): refactor
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
3a30260c85
commit
da7c05bd1b
1 changed files with 20 additions and 19 deletions
|
@ -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 {
|
|||
}
|
||||
}
|
||||
|
||||
type Input = Vec<Assignment>;
|
||||
type Output = i32;
|
||||
impl Assignment {
|
||||
fn fully_overlap(&self) -> bool {
|
||||
let Assignment(l, r) = self;
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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<Assignment>;
|
||||
type Output = usize;
|
||||
|
||||
fn part_1(input: &Input) -> Output {
|
||||
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 {
|
||||
|
|
Loading…
Reference in a new issue