Added Solution for 2020 day 06
This commit is contained in:
parent
1f84b3496a
commit
cec9237636
5 changed files with 2288 additions and 0 deletions
8
2020/day06_custom_customs/Cargo.toml
Normal file
8
2020/day06_custom_customs/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day06_custom_customs"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
97
2020/day06_custom_customs/challenge.txt
Normal file
97
2020/day06_custom_customs/challenge.txt
Normal file
|
@ -0,0 +1,97 @@
|
|||
As your flight approaches the regional airport where you'll switch to a much larger plane, [customs declaration forms](https://en.wikipedia.org/wiki/Customs_declaration) are distributed to the passengers.
|
||||
|
||||
The form asks a series of 26 yes-or-no questions marked `a` through `z`. All you need to do is identify the questions for which *anyone in your group* answers "yes". Since your group is just you, this doesn't take very long.
|
||||
|
||||
However, the person sitting next to you seems to be experiencing a language barrier and asks if you can help. For each of the people in their group, you write down the questions for which they answer "yes", one per line. For example:
|
||||
|
||||
```
|
||||
abcx
|
||||
abcy
|
||||
abcz
|
||||
|
||||
```
|
||||
|
||||
In this group, there are *`6`* questions to which anyone answered "yes": `a`, `b`, `c`, `x`, `y`, and `z`. (Duplicate answers to the same question don't count extra; each question counts at most once.)
|
||||
|
||||
Another group asks for your help, then another, and eventually you've collected answers from every group on the plane (your puzzle input). Each group's answers are separated by a blank line, and within each group, each person's answers are on a single line. For example:
|
||||
|
||||
```
|
||||
abc
|
||||
|
||||
a
|
||||
b
|
||||
c
|
||||
|
||||
ab
|
||||
ac
|
||||
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
|
||||
b
|
||||
|
||||
```
|
||||
|
||||
This list represents answers from five groups:
|
||||
|
||||
* The first group contains one person who answered "yes" to *`3`* questions: `a`, `b`, and `c`.
|
||||
* The second group contains three people; combined, they answered "yes" to *`3`* questions: `a`, `b`, and `c`.
|
||||
* The third group contains two people; combined, they answered "yes" to *`3`* questions: `a`, `b`, and `c`.
|
||||
* The fourth group contains four people; combined, they answered "yes" to only *`1`* question, `a`.
|
||||
* The last group contains one person who answered "yes" to only *`1`* question, `b`.
|
||||
|
||||
In this example, the sum of these counts is `3 + 3 + 3 + 1 + 1` = *`11`*.
|
||||
|
||||
For each group, count the number of questions to which anyone answered "yes". *What is the sum of those counts?*
|
||||
|
||||
Your puzzle answer was `6521`.
|
||||
|
||||
\--- 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?*
|
||||
|
||||
Your puzzle answer was `3305`.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: \*\*
|
||||
|
||||
At this point, you should [return to your Advent calendar](/2020) and try another puzzle.
|
||||
|
||||
If you still want to see it, you can [get your puzzle input](6/input).
|
36
2020/day06_custom_customs/src/lib.rs
Normal file
36
2020/day06_custom_customs/src/lib.rs
Normal file
|
@ -0,0 +1,36 @@
|
|||
pub fn run(input: &str) -> (usize, usize) {
|
||||
let groups: Vec<_> = input.split("\n\n").collect();
|
||||
let first = groups.iter().map(|group| count_unique_chars(group)).sum();
|
||||
let second = groups.iter().map(|group| count_common_chars(group)).sum();
|
||||
(first, second)
|
||||
}
|
||||
|
||||
fn count_unique_chars(input: &str) -> usize {
|
||||
('a'..='z').filter(|c| input.contains(*c)).count()
|
||||
}
|
||||
|
||||
fn count_common_chars(input: &str) -> usize {
|
||||
('a'..='z').filter(|c| input.lines().all(|line| line.contains(*c))).count()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fs::read_to_string;
|
||||
|
||||
fn read_file(name: &str) -> String {
|
||||
read_to_string(name).expect(&format!("Unable to read file: {name}")[..]).trim().to_string()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sample() {
|
||||
let sample_input = read_file("tests/sample_input");
|
||||
assert_eq!(run(&sample_input), (11, 6));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_challenge() {
|
||||
let challenge_input = read_file("tests/challenge_input");
|
||||
assert_eq!(run(&challenge_input), (6521, 3305));
|
||||
}
|
||||
}
|
2132
2020/day06_custom_customs/tests/challenge_input
Normal file
2132
2020/day06_custom_customs/tests/challenge_input
Normal file
File diff suppressed because it is too large
Load diff
15
2020/day06_custom_customs/tests/sample_input
Normal file
15
2020/day06_custom_customs/tests/sample_input
Normal file
|
@ -0,0 +1,15 @@
|
|||
abc
|
||||
|
||||
a
|
||||
b
|
||||
c
|
||||
|
||||
ab
|
||||
ac
|
||||
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
|
||||
b
|
Loading…
Add table
Add a link
Reference in a new issue