Advent of Code 2020 - Day 6Custom Customs

Python | Problem statement | Source code | Tags: Data structures

Part 1

Each group's answers is a list of sets. For part 1, if we want the questions anyone answered "yes", that's the union of the sets in each list. Conveniently we can simply concatenate all the strings in the group (removing newlines) and convert that to a set, which automatically removes duplicates.

sum(len(set(g.replace("\n", ""))) for g in groups)
python

Part 2

Now we want the questions everyone answered "yes", which is the intersection of the sets in each list. Python's set.intersection method can take multiple arguments, so we can unpack the list of sets with *:

sum(len(set.intersection(*map(set, g.split("\n")))) for g in groups)
python