Advent of Code: Day #06 - Custom Customs
4 minutes
Introduction
I decided to challenge myself today and see just how small I could make my solutions. I think you will agree I got them pretty damn small!
Task One
For task one we we're given a list of new line separated inputs. Each input represented a group of passengers where each passenger was on a new line. These passengers have answered a series of 26 yes-no questions. The task for today is to sum up the amount of unique questions answered by the group (So if there were two passengers in a group and they both answered A we would only count this once.)
Reading in the data
I decided to read the data into an array of sub arrays where each element in the sub array contained the answers of a passenger in the group. I did this by reading in all the data then splitting it on double new lines to get each group into its own array and then split again on a single new line to separate group by passenger.
def read_data(path):with open(path, "r") as file:data = file.read()return [i.split("\n") for i in data.split("\n\n")]
The Solution
I combined all of the group's passenger's answers into one string and then converted the string to a set to remove duplicates. I then take the length of this set to find how many questions the group answered. We do this for all groups and then take the sum.
def task_one(questions):return sum([len(set("".join(i))) for i in questions])
Task Two
The Solution
Task two asks us, instead, to find the answers all of the passengers in a group answered. To accomplish this I made each passenger in a groups answers into a set and then found the intersection (Remove items that aren't the same) of all of a groups passengers answers. This got me a set of answers that all passengers in a group answered. From there it was just taking the length again and then returning the sum for all groups.
def task_two(questions):return sum([len(reduce(set.intersection, [set(i) for i in group]))for group in questions])
Links
Leaderboard Code: 353270-1fc6ef28
Github Repository A link to the github repo containing all the days.
main.py A direct download of the main.py script
618B