Day 6 part 2.
This commit is contained in:
parent
60a4950f24
commit
89e6dfe8be
@ -38,6 +38,38 @@
|
||||
//!
|
||||
//! For each group, count the number of questions to which anyone answered "yes". What is the sum of those counts?
|
||||
//!
|
||||
//! --- Part Two ---
|
||||
//! As you finish the last group's customs declaration, you notice that you misread one word in the instructions:
|
||||
//!
|
||||
//! You don't need to identify the questions to which anyone answered "yes"; you need to identify the questions to which everyone answered "yes"!
|
||||
//!
|
||||
//! Using the same example as above:
|
||||
//!
|
||||
//! abc
|
||||
//!
|
||||
//! a
|
||||
//! b
|
||||
//! c
|
||||
//!
|
||||
//! ab
|
||||
//! ac
|
||||
//!
|
||||
//! a
|
||||
//! a
|
||||
//! a
|
||||
//! a
|
||||
//!
|
||||
//! b
|
||||
//! This list represents answers from five groups:
|
||||
//!
|
||||
//! In the first group, everyone (all 1 person) answered "yes" to 3 questions: a, b, and c.
|
||||
//! In the second group, there is no question to which everyone answered "yes".
|
||||
//! In the third group, everyone answered yes to only 1 question, a. Since some people did not answer "yes" to b or c, they don't count.
|
||||
//! In the fourth group, everyone answered yes to only 1 question, a.
|
||||
//! In the fifth group, everyone (all 1 person) answered "yes" to 1 question, b.
|
||||
//! In this example, the sum of these counts is 3 + 0 + 1 + 1 + 1 = 6.
|
||||
//!
|
||||
//! For each group, count the number of questions to which everyone answered "yes". What is the sum of those counts?
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
@ -52,6 +84,24 @@ fn solution1(input: &str) -> usize {
|
||||
.sum()
|
||||
}
|
||||
|
||||
#[aoc(day6, part2)]
|
||||
fn solution2(input: &str) -> usize {
|
||||
input
|
||||
.split("\n\n")
|
||||
.map(|group| {
|
||||
let sets = group
|
||||
.split('\n')
|
||||
.map(|p| p.chars().collect::<HashSet<_>>())
|
||||
.collect::<Vec<_>>();
|
||||
// Find letters common to all sets int this group.
|
||||
sets.iter().fold(sets.first().unwrap().clone(), |acc, s| {
|
||||
acc.intersection(s).cloned().collect()
|
||||
})
|
||||
})
|
||||
.map(|set| set.len())
|
||||
.sum()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@ -76,4 +126,9 @@ b"#;
|
||||
fn part1() {
|
||||
assert_eq!(solution1(INPUT), 11);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2() {
|
||||
assert_eq!(solution2(INPUT), 6);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user