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)); + } }