Advent of Code, Day 6

Day 6 of Advent of Code is here! Check out Day 1 for what this is all about. See all of my solutions here.

Did you join my leaderboard? I invite you to join my board, and we can see who can win it ;) It’s not too late, go to “Leaderboard” in the top nav of AoC and enter 1030369-e174b794 to join my board.

Get your leaderboard in Slack! See my other post, written by a friend of mine, on how to get your leaderboard into your Slack channel with a fun lambda function and slash command!

I have also loaded up just the challenge and code from this and my other days on github, you can follow along there too: pretty Git pages or the code in the repo.

Don’t cheat!

I highly encourage you to get through the day’s challenge on your own. I would love to hear how you did! Did you find a better approach than I did? Do tell!

Day 6: Custom Customs

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

The groups of answers look like this

Remember, a letter above indicates that person said Yes to that question.

As always, we need to get all of the data:

var answers = document.getElementsByTagName('pre')[0].innerText.replace(/\n\n/ig,'LOZZI').replace(/\n/ig,'').split('LOZZI')

As mentioned in Day 5, I’ve relied on chaining a few different methods together here to have 1 line of code. The above consolidates the multiple rows and groups into an array, where each item in the array is one group:

We got our answers, so lets find how many questions were answered with a yes

var yesCounter = 0
answers.forEach(a => {
  yesCounter += new Set(a).size
})
console.log(yesCounter)

Super simple, right? As we loop through each set of answers we do this magic thing with Set and size. I, too, just learned what this is. A Set enforces the uniqueness of the array, there can only be unique values in a Set. Size simply returns the size of the set, similar to an array.length.

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

always make sure to read all the instructions, all the way through, gah, I can’t believe I misread it :D

Up in Part I, I had that long one liner which gets the data and does some prepping of it. We have to start over for Part II, but that’s okay, we’re agile. This will be a little simpler than above.

var groups = document.getElementsByTagName('pre')[0].innerText.split('\n\n')

Then we loop through the groups, then loop through each answer and find the unique ones

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)

Well, we’re not actually looping through each answer, that would be too much work. The need is to find only the answers that were Yes across all of the people. So, if the first answer doesn’t have it, why bother moving to the second answer?

  • Within looping the groups, we split the group by the line return \n. This will get us an array of each unique set of answers
  • Grab the first answer in the group
  • Loop through the letters in the first answer and check if every answer in this group has that letter, if so, increment our counter
    • How is that incrementing? Fortunately, true and false translate to numbers, 1 and 0 to be precise. So a true will add 1 and a false adds 0

How did it go for you?

How did you find the answer on your own? How did you do it? Anything stump you? I’d love to hear how you did! Please comment below!

Series Navigation<< Advent of Code, Day 7Advent of Code, Day 5 >>

One thought on “Advent of Code, Day 6

Add yours

Leave a Reply

Up ↑

Discover more from David Lozzi

Subscribe now to keep reading and get access to the full archive.

Continue reading