main
idylls 1 year ago
parent 9719b895ce
commit 7479e8adbd
Signed by: idylls
GPG Key ID: 8A7167CBC2CC9F0F

@ -0,0 +1,69 @@
use crate::util::prelude::*;
static INPUT: &str = include_str!("inputs/day4.txt");
fn parse_range(s: &str) -> core::ops::RangeInclusive<uint> {
let mut parts = s.split('-');
let lo = parts.next().unwrap();
let hi = parts.next().unwrap();
let lo = lo.parse().unwrap();
let hi = hi.parse().unwrap();
lo..=hi
}
fn fully_contains<U: PartialOrd>(
haystack: &core::ops::RangeInclusive<U>,
needle: &core::ops::RangeInclusive<U>,
) -> bool {
haystack.contains(needle.start()) && haystack.contains(needle.end())
}
fn has_overlap<U: PartialOrd>(
a: &core::ops::RangeInclusive<U>,
b: &core::ops::RangeInclusive<U>,
) -> bool {
a.contains(b.start())
|| a.contains(b.end())
|| b.contains(a.start())
|| b.contains(a.end())
}
pub fn part1() {
let sum = INPUT
.split('\n')
.filter(|s| !s.is_empty())
.map(|line| {
let mut elves = line.split(',');
let fst = elves.next().unwrap();
let fst = parse_range(fst);
let snd = elves.next().unwrap();
let snd = parse_range(snd);
u64::from(fully_contains(&fst, &snd) || fully_contains(&snd, &fst))
})
.sum::<uint>();
println!("{}", sum);
}
pub fn part2() {
let sum = INPUT
.split('\n')
.filter(|s| !s.is_empty())
.map(|line| {
let mut elves = line.split(',');
let fst = elves.next().unwrap();
let fst = parse_range(fst);
let snd = elves.next().unwrap();
let snd = parse_range(snd);
u64::from(has_overlap(&fst, &snd))
})
.sum::<uint>();
println!("{}", sum);
}

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save