From 63eaad3ad273cae5508168312baf4ace31f69ddc Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sat, 3 Dec 2022 23:07:30 -0800 Subject: [PATCH] add alternate solutions with tuples instead of ranges --- 2022-aoc/src/d4.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/2022-aoc/src/d4.rs b/2022-aoc/src/d4.rs index 6ad9ce0..2b4a1f4 100644 --- a/2022-aoc/src/d4.rs +++ b/2022-aoc/src/d4.rs @@ -23,6 +23,30 @@ fn parse_input(input: &str) -> Vec { out } +// with tuples instead of ranges +type AltContents = ((u32, u32), (u32, u32)); + +#[aoc_generator(day4, part1, alt1)] +fn parse_input1a(input: &str) -> Vec { + let mut out = Vec::with_capacity(input.len()); + for line in input.lines() { + let (a, b) = line.split_once(',').unwrap(); + let (alo, ahi) = a.split_once('-').unwrap(); + let (alo, ahi) = (alo.parse().unwrap(), ahi.parse().unwrap()); + let (blo, bhi) = b.split_once('-').unwrap(); + let (blo, bhi) = (blo.parse().unwrap(), bhi.parse().unwrap()); + out.push(((alo, ahi), (blo, bhi))); + } + + out +} + +// can't define a blanket alt generator? +#[aoc_generator(day4, part2, alt1)] +fn p2a(input: &str) -> Vec { + parse_input1a(input) +} + #[aoc_run(day4, part1)] fn part1(scheds: &[Contents]) -> u32 { let mut out = 0; @@ -48,3 +72,25 @@ fn part2(scheds: &[Contents]) -> u32 { } out } + +#[aoc_run(day4, part1, alt1)] +fn part1a(scheds: &[AltContents]) -> u32 { + let mut out = 0; + for (a, b) in scheds { + if (a.0 <= b.0 && b.1 <= a.1) || (b.0 <= a.0 && a.1 <= b.1) { + out += 1; + } + } + out +} + +#[aoc_run(day4, part2, alt1)] +fn part2a(scheds: &[AltContents]) -> u32 { + let mut out = 0; + for (a, b) in scheds { + if (a.0 <= b.0 && b.0 <= a.1) || (b.0 <= a.0 && a.0 <= b.1) { + out += 1; + } + } + out +}