From 0e475d9264b1147d02f78c6dc5a93010d94ccaa2 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sun, 4 Dec 2022 23:46:26 -0500 Subject: [PATCH] Clean up the counters --- 2022/src/day4.rs | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/2022/src/day4.rs b/2022/src/day4.rs index 336ad76..5b6c47b 100644 --- a/2022/src/day4.rs +++ b/2022/src/day4.rs @@ -4,34 +4,25 @@ use std::collections::HashSet; const INPUT: &str = include_str!("../data/day4.txt"); pub fn part1() -> String { - format!("{}", count_full_containments(input(INPUT))) + format!("{}", count_full_containments(&input(INPUT))) } pub fn part2() -> String { - format!("{}", count_overlaps(input(INPUT))) + format!("{}", count_overlaps(&input(INPUT))) } -fn count_full_containments(pairings: Vec) -> u32 { - pairings.into_iter().fold(0, |cur, pairing| { - if pairing.has_full_containment() { - cur + 1 - } else { - cur - } - }) +fn count_full_containments(pairings: &Vec) -> u32 { + pairings + .into_iter() + .filter(|pairing| pairing.has_full_containment()) + .count() as u32 } -fn count_overlaps(pairings: Vec) -> u32 { - pairings.into_iter().fold( - 0, - |cur, pairing| { - if pairing.has_overlap() { - cur + 1 - } else { - cur - } - }, - ) +fn count_overlaps(pairings: &Vec) -> u32 { + pairings + .into_iter() + .filter(|pairing| pairing.has_overlap()) + .count() as u32 } struct ElfPairing { @@ -132,7 +123,7 @@ mod test { #[test] fn it_counts_full_containments() { with_input(|pairings| { - assert_eq!(2, count_full_containments(pairings)); + assert_eq!(2, count_full_containments(&pairings)); }); } @@ -147,4 +138,11 @@ mod test { assert!(pairings[5].has_overlap()); }); } + + #[test] + fn it_checks_part1() { + let pairings = input(INPUT); + assert_eq!(466, count_full_containments(&pairings)); + assert_eq!(865, count_overlaps(&pairings)); + } }