2022-12-04 06:29:07 +00:00
|
|
|
use aoc_runner_derive::{aoc as aoc_run, aoc_generator};
|
|
|
|
|
2022-12-04 19:55:31 +00:00
|
|
|
type Assignments = ((u32, u32), (u32, u32));
|
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-04 19:55:31 +00:00
|
|
|
let (a, b) = (get_range(a), get_range(b));
|
|
|
|
out.push((a, b));
|
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:55:31 +00:00
|
|
|
fn get_range(r: &str) -> (u32, u32) {
|
|
|
|
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:55:31 +00:00
|
|
|
fn contains(a: &(u32, u32), b: &(u32, u32)) -> bool {
|
|
|
|
a.0 <= b.0 && b.1 <= a.1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn overlaps(a: &(u32, u32), b: &(u32, u32)) -> bool {
|
|
|
|
a.0 <= b.0 && b.0 <= a.1
|
2022-12-04 07:07:30 +00:00
|
|
|
}
|