Advent of Code Day 6

So day 6 of Advent of Code was looking for you to help out a planeload of fliers with their customs forms. Our input file was a multi-line file where each line represented an individual person with groups separated by an empty line. Each line contains a series of letters to represent questions (gotta love customs). We’re trying to figure out how many questions each group answered.

To solve this problem, I decided to make use of the defaultdict function. This is a function I’ve used before a few times, but honestly not a lot. I love how it works though because it allows me to create a dict of just unique values. In this case, I had a feeling it keeping track of the number of people who answered each question may be useful in the 2nd part. So I had my defaultdict initialize an integer of 0 so I could add to each it each time a question came up.

Once I got to end of each group, I appended the defaultdict to list (list of groups) and reinitialized the defaultdict. Once it was done, I just cycled through the list of dicts and got the length of each dict.

Part two changed it around a little bit and rather than counting the questions that anyone answered yes to, it wanted the questions that everyone within a group answered yes to.

This just required a little change to my existing code for part one. I went ahead started to track the number of people within each group. Once I reached the end of the group, I passed the size of the group along with the defaultdict to a function that looked for any key within the dict that contained a value equal to the size of the group and returned the number.

You can find the complete solution here.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>