Clean up the counters

This commit is contained in:
Savanni D'Gerinel 2022-12-04 23:46:26 -05:00
parent 3e3fbd802d
commit 0e475d9264
1 changed files with 20 additions and 22 deletions

View File

@ -4,34 +4,25 @@ use std::collections::HashSet;
const INPUT: &str = include_str!("../data/day4.txt"); const INPUT: &str = include_str!("../data/day4.txt");
pub fn part1() -> String { pub fn part1() -> String {
format!("{}", count_full_containments(input(INPUT))) format!("{}", count_full_containments(&input(INPUT)))
} }
pub fn part2() -> String { pub fn part2() -> String {
format!("{}", count_overlaps(input(INPUT))) format!("{}", count_overlaps(&input(INPUT)))
} }
fn count_full_containments(pairings: Vec<ElfPairing>) -> u32 { fn count_full_containments(pairings: &Vec<ElfPairing>) -> u32 {
pairings.into_iter().fold(0, |cur, pairing| { pairings
if pairing.has_full_containment() { .into_iter()
cur + 1 .filter(|pairing| pairing.has_full_containment())
} else { .count() as u32
cur
}
})
} }
fn count_overlaps(pairings: Vec<ElfPairing>) -> u32 { fn count_overlaps(pairings: &Vec<ElfPairing>) -> u32 {
pairings.into_iter().fold( pairings
0, .into_iter()
|cur, pairing| { .filter(|pairing| pairing.has_overlap())
if pairing.has_overlap() { .count() as u32
cur + 1
} else {
cur
}
},
)
} }
struct ElfPairing { struct ElfPairing {
@ -132,7 +123,7 @@ mod test {
#[test] #[test]
fn it_counts_full_containments() { fn it_counts_full_containments() {
with_input(|pairings| { 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()); 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));
}
} }