From 8dc7f8bf530f8704a4a0fb1e0cc857590fe4bc3d Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sat, 3 Dec 2022 22:29:07 -0800 Subject: [PATCH] part2 done --- 2022-aoc/src/d4.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 2022-aoc/src/d4.rs diff --git a/2022-aoc/src/d4.rs b/2022-aoc/src/d4.rs new file mode 100644 index 0000000..6ad9ce0 --- /dev/null +++ b/2022-aoc/src/d4.rs @@ -0,0 +1,50 @@ +use std::ops::RangeInclusive; + +use aoc_runner_derive::{aoc as aoc_run, aoc_generator}; + +type Contents = (RangeInclusive, RangeInclusive); + +#[aoc_generator(day4)] +fn parse_input(input: &str) -> Vec { + 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 +}