1
0
Fork 0

day(04): refactor

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2022-12-04 14:37:18 +01:00
parent 3a30260c85
commit da7c05bd1b
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

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