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::str::FromStr;
|
||||||
use std::{collections::HashSet, ops::RangeBounds};
|
|
||||||
|
|
||||||
use aoc_2022::*;
|
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<Assignment>;
|
type Input = Vec<Assignment>;
|
||||||
type Output = i32;
|
type Output = usize;
|
||||||
|
|
||||||
fn part_1(input: &Input) -> Output {
|
fn part_1(input: &Input) -> Output {
|
||||||
input
|
input.iter().filter(|a| a.fully_overlap()).count()
|
||||||
.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 part_2(input: &Input) -> Output {
|
fn part_2(input: &Input) -> Output {
|
||||||
input
|
input.iter().filter(|a| a.overlap()).count()
|
||||||
.iter()
|
|
||||||
.filter(|Assignment(l, r)| {
|
|
||||||
l.contains(r.start())
|
|
||||||
|| l.contains(r.end())
|
|
||||||
|| r.contains(l.start())
|
|
||||||
|| r.contains(l.end())
|
|
||||||
})
|
|
||||||
.count() as i32
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_input(pathname: &str) -> Input {
|
fn parse_input(pathname: &str) -> Input {
|
||||||
|
|
Loading…
Reference in a new issue