2022-12-04 06:29:07 +00:00
|
|
|
use aoc_runner_derive::{aoc as aoc_run, aoc_generator};
|
|
|
|
|
2022-12-04 19:58:25 +00:00
|
|
|
type TRange = (u32, u32);
|
|
|
|
type Assignments = (TRange, TRange);
|
2022-12-04 06:29:07 +00:00
|
|
|
|
|
|
|
#[aoc_generator(day4)]
|
2022-12-04 19:55:31 +00:00
|
|
|
fn parse_input(input: &str) -> Vec<Assignments> {
|
2022-12-04 07:07:30 +00:00
|
|
|
let mut out = Vec::with_capacity(input.len());
|
|
|
|
for line in input.lines() {
|
|
|
|
let (a, b) = line.split_once(',').unwrap();
|
2022-12-05 22:01:03 +00:00
|
|
|
let assignment = (get_range(a), get_range(b));
|
|
|
|
out.push(assignment);
|
2022-12-04 07:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:29:07 +00:00
|
|
|
#[aoc_run(day4, part1)]
|
2022-12-04 19:55:31 +00:00
|
|
|
fn part1(scheds: &[Assignments]) -> u32 {
|
2022-12-04 06:29:07 +00:00
|
|
|
let mut out = 0;
|
|
|
|
for (a, b) in scheds {
|
2022-12-04 19:55:31 +00:00
|
|
|
if contains(a, b) || contains(b, a) {
|
2022-12-04 06:29:07 +00:00
|
|
|
out += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out
|
|
|
|
}
|
|
|
|
|
|
|
|
#[aoc_run(day4, part2)]
|
2022-12-04 19:55:31 +00:00
|
|
|
fn part2(scheds: &[Assignments]) -> u32 {
|
2022-12-04 06:29:07 +00:00
|
|
|
let mut out = 0;
|
|
|
|
for (a, b) in scheds {
|
2022-12-04 19:55:31 +00:00
|
|
|
if overlaps(a, b) || overlaps(b, a) {
|
2022-12-04 06:29:07 +00:00
|
|
|
out += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out
|
|
|
|
}
|
2022-12-04 07:07:30 +00:00
|
|
|
|
2022-12-04 19:58:25 +00:00
|
|
|
fn get_range(r: &str) -> TRange {
|
2022-12-04 19:55:31 +00:00
|
|
|
let (lo, hi) = r.split_once('-').unwrap();
|
|
|
|
(lo.parse().unwrap(), hi.parse().unwrap())
|
2022-12-04 07:07:30 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 19:58:25 +00:00
|
|
|
fn contains(a: &TRange, b: &TRange) -> bool {
|
2022-12-04 19:55:31 +00:00
|
|
|
a.0 <= b.0 && b.1 <= a.1
|
|
|
|
}
|
|
|
|
|
2022-12-04 19:58:25 +00:00
|
|
|
fn overlaps(a: &TRange, b: &TRange) -> bool {
|
2022-12-04 19:55:31 +00:00
|
|
|
a.0 <= b.0 && b.0 <= a.1
|
2022-12-04 07:07:30 +00:00
|
|
|
}
|