2020: day06: part 2

This commit is contained in:
Antoine Martin 2020-12-06 12:48:22 +01:00
parent bc0ff1b150
commit 60715cad1a

View file

@ -7,6 +7,7 @@ pub fn run() -> aoc::Result<String> {
let mut res = String::with_capacity(128); let mut res = String::with_capacity(128);
writeln!(res, "part 1: {}", part1(INPUT)?)?; writeln!(res, "part 1: {}", part1(INPUT)?)?;
writeln!(res, "part 2: {}", part2(INPUT)?)?;
Ok(res) Ok(res)
} }
@ -49,6 +50,24 @@ fn part1(input: &str) -> aoc::Result<usize> {
.sum()) .sum())
} }
fn part2(input: &str) -> aoc::Result<usize> {
let groups = get_groups(input)?;
Ok(groups
.iter()
.map(|group| {
group
.answers
.iter()
.skip(1)
.fold(group.answers[0].clone(), |set, answers| {
set.intersection(answers).copied().collect()
})
})
.map(|set| set.len())
.sum())
}
struct Group { struct Group {
answers: Vec<HashSet<u8>>, answers: Vec<HashSet<u8>>,
} }
@ -68,4 +87,14 @@ mod tests {
fn part1_real() { fn part1_real() {
assert_eq!(part1(INPUT).unwrap(), 6382); assert_eq!(part1(INPUT).unwrap(), 6382);
} }
#[test]
fn part2_provided() {
assert_eq!(part2(PROVIDED).unwrap(), 6);
}
#[test]
fn part2_real() {
assert_eq!(part2(INPUT).unwrap(), 3197);
}
} }