diff options
Diffstat (limited to '2020/day-06/README.org')
-rw-r--r-- | 2020/day-06/README.org | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/2020/day-06/README.org b/2020/day-06/README.org new file mode 100644 index 0000000..508800c --- /dev/null +++ b/2020/day-06/README.org @@ -0,0 +1,152 @@ +#+SETUPFILE: ~/.emacs.d/org-templates/level-3.org +#+HTML_LINK_UP: ../../index.html#2020 +#+OPTIONS: toc:1 +#+EXPORT_FILE_NAME: index +#+TITLE: Day 06 - Custom Customs + +* Puzzle +- This puzzle is taken from: https://adventofcode.com/2020/day/6 + +As your flight approaches the regional airport where you'll switch to a +much larger plane, customs declaration forms 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: +#+BEGIN_SRC +abcx +abcy +abcz +#+END_SRC + + +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: +#+BEGIN_SRC +abc + +a +b +c + +ab +ac + +a +a +a +a + +b +#+END_SRC + +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? +** Part 2 +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: +#+BEGIN_SRC +abc + +a +b +c + +ab +ac + +a +a +a +a + +b +#+END_SRC + +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? +* Solution +The answers are stored in =@answers= & =$count= is kept for keeping the sum +of counts. + +=%yes= is a hash that'll store the number of times each answer was marked +"yes". =@individual_answers= holds each individual's answer from that +group. +#+BEGIN_SRC raku +my $input = slurp "input"; +my @answers = $input.chomp.split("\n\n"); +my $count = 0; + +for @answers -> $answer { + my %yes; + my @individual_answers = $answer.split("\n"); + %yes{$_} += 1 for @individual_answers.join.comb; + + if $part == 1 { + ... + } elsif $part == 2 { + ... + } +} +say "Part $part: ", $count; +#+END_SRC + +For part 1, we just set =$count= to number of keys in =%yes=. +#+BEGIN_SRC raku +$count += keys %yes; +#+END_SRC +** Part 2 +For part 2, iterate over keys of =%yes= & increment =$count= by 1 if that +answer is marked yes by all individuals. +#+BEGIN_SRC raku +for keys %yes { + $count += 1 if %yes{$_} eq @individual_answers.elems; +} +#+END_SRC |