Day 6 - Custom Customs - Code
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:
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
, andc
. - The second group contains three people; combined, they answered "yes" to 3 questions:
a
,b
, andc
. - The third group contains two people; combined, they answered "yes" to 3 questions:
a
,b
, andc
. - 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?
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
, andc
. - 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" tob
orc
, 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?
Part two comes down to finding an intersection between N
arrays. What
simplifies the solution is that each array does not have any duplicates.
/**
* This approach converts each person in a group to a Set, and then we reduce
* the array of Sets into a single set. If the finalSet has the answer we're
* currently working on, then we keep it in the finalSet. Otherwise, we take it
* out.
*/
function getSimulationsBAgain(): Simulation[] {
return getRunsFromIniFile(input).map((sim) => ({
name: sim.name,
groups: sim.content.split('\n\n').map((g) => {
return g
.split('\n')
.filter((p) => p.trim() !== '')
.map((p) => new Set(p))
.reduce(
(finalSet, currSet) =>
new Set([...finalSet].filter((a) => currSet.has(a)))
);
// new learning: if you don't include a second parameter to reduce,
// it uses the first value in the array as the initial value. :O
}),
}));
}
Even if the arrays did have duplicates, we could probably represent each array
as a Map<string, count>
, where the value is the number of times each letter
appears.
I had originally did it a bit differently:
/**
* This approach goes through each person in a group, and storing the answers in
* a Map<answer, count>. If the count === # of people, then we keep the answer
* in the intersected set.
*/
function getSimulationsB(): Simulation[] {
return getRunsFromIniFile(input).map((sim) => ({
name: sim.name,
groups: sim.content.split('\n\n').map((g) => {
// E.g., people = ['abc', 'abd', 'ab', 'b'];
const people = g.split('\n').filter((p) => p.trim() !== '');
// get a count of all the individual answers we got.
const answerCount = people.reduce((map, answers) => {
for (const ans of answers) {
const count = map.get(ans) ?? 0;
map.set(ans, count + 1);
}
return map;
}, new Map<string, number>());