aoc2022/src/days/day4.rs
2022-12-04 11:27:16 -06:00

39 lines
1 KiB
Rust

use std::ops::Range;
use aoc_runner_derive::{aoc, aoc_generator};
#[aoc_generator(day4)]
fn generator(s: &str) -> Vec<(Range<u32>, Range<u32>)> {
s.lines()
.map(|l| {
let (a, b) = l.split_once(',').unwrap();
let a = a.split_once('-').unwrap();
let b = b.split_once('-').unwrap();
(
a.0.parse().unwrap()..a.1.parse().unwrap(),
b.0.parse().unwrap()..b.1.parse().unwrap(),
)
})
.collect()
}
#[aoc(day4, part1)]
fn part1(input: &[(Range<u32>, Range<u32>)]) -> u32 {
input
.iter()
.filter(|(a, b)| {
(a.start <= b.start && a.end >= b.end) || (a.start >= b.start && a.end <= b.end)
})
.count() as u32
}
#[aoc(day4, part2)]
fn part2(input: &[(Range<u32>, Range<u32>)]) -> u32 {
input
.iter()
.filter(|(a, b)| {
(a.start <= b.end && a.start >= b.start) || (b.start <= a.end && b.start >= a.start)
})
.count() as u32
}