Advent of Code, Day 6: Custom Customs
Go to Day 6 Challenge |
Go to index
I ran this code in the devtoolbar in Chrome, and the answers were simply outputted to the console.
Learn more about my solution.
Part I
"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"... For each of the people in their group, you
write down the questions for which they answer "yes", one per line... 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."
ref
var yesCounter = 0
var answers = document.getElementsByTagName('pre')[0].innerText.replace(/\n\n/ig,'LOZZI').replace(/\n/ig,'').split('LOZZI')
answers.forEach(a => {
yesCounter += new Set(a).size
})
console.log(yesCounter)
Part II
"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"!"
ref
var groups = document.getElementsByTagName('pre')[0].innerText.split('\n\n')
var sameAnswerCounter = 0
groups.forEach(group => {
var answers = group.split('\n')
var firstAnswer = answers[0];
[...firstAnswer].forEach(f => {
sameAnswerCounter += answers.every(a => a.indexOf(f) > -1)
})
})
console.log(sameAnswerCounter)