dumb_shit/2022-aoc/src/d4.rs

51 lines
1.3 KiB
Rust
Raw Normal View History

2022-12-04 06:29:07 +00:00
use std::ops::RangeInclusive;
use aoc_runner_derive::{aoc as aoc_run, aoc_generator};
type Contents = (RangeInclusive<u32>, RangeInclusive<u32>);
#[aoc_generator(day4)]
fn parse_input(input: &str) -> Vec<Contents> {
let mut out = Vec::with_capacity(input.len());
for line in input.lines() {
let mut split = line.split(',');
let a = split.next().unwrap();
let b = split.next().unwrap();
let mut asplit = a.split('-');
let mut bsplit = b.split('-');
let alo = asplit.next().unwrap().parse().unwrap();
let ahi = asplit.next().unwrap().parse().unwrap();
let blo = bsplit.next().unwrap().parse().unwrap();
let bhi = bsplit.next().unwrap().parse().unwrap();
out.push(((alo..=ahi), (blo..=bhi)));
}
out
}
#[aoc_run(day4, part1)]
fn part1(scheds: &[Contents]) -> u32 {
let mut out = 0;
for (a, b) in scheds {
if (a.contains(b.start()) && a.contains(b.end()))
|| (b.contains(a.start()) && b.contains(a.end()))
{
out += 1;
}
}
out
}
#[aoc_run(day4, part2)]
fn part2(scheds: &[Contents]) -> u32 {
let mut out = 0;
for (a, b) in scheds {
if (a.contains(b.start()) || a.contains(b.end()))
|| (b.contains(a.start()) || b.contains(a.end()))
{
out += 1;
}
}
out
}