From 5eb58ad076f2cd435b11b140820da224b60b73d5 Mon Sep 17 00:00:00 2001 From: Aria Date: Mon, 2 Jan 2023 21:58:56 +0000 Subject: initial commit --- 2022/src/day04.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 2022/src/day04.rs (limited to '2022/src/day04.rs') diff --git a/2022/src/day04.rs b/2022/src/day04.rs new file mode 100644 index 0000000..052dde1 --- /dev/null +++ b/2022/src/day04.rs @@ -0,0 +1,35 @@ +mod utils; + +use std::ops::RangeInclusive; + +use utils::{iter_pair, read_input}; + +fn main() { + let input = read_input(); + + let pairs = input.lines().map(|x| { + iter_pair(x.split(',').map(|x| -> RangeInclusive { + let (start, end) = iter_pair(x.split('-')); + start.parse().unwrap()..=end.parse().unwrap() + })) + }); + + let (fully_contains, partially_contains) = pairs + .map(|(a, b)| (fully_overlap(&a, &b), partially_overlap(&a, &b))) + .fold((0, 0), |(acc_full, acc_part), (full, part)| { + (acc_full + full as usize, acc_part + part as usize) + }); + + println!("Part 1: {}", fully_contains); + println!("Part 2: {}", partially_contains); +} + +#[inline(always)] +fn fully_overlap(a: &RangeInclusive, b: &RangeInclusive) -> bool { + (a.contains(b.start()) && a.contains(b.end())) || (b.contains(a.start()) && b.contains(a.end())) +} + +#[inline(always)] +fn partially_overlap(a: &RangeInclusive, b: &RangeInclusive) -> bool { + a.contains(b.start()) || a.contains(b.end()) || b.contains(a.start()) || b.contains(a.end()) +} -- cgit v1.2.3