Advent Of Code 2020 Day 6: Game, Sets, Match
The Day 6 puzzle is trivially solved using sets. The input is a list of groups, and the answers given by each member of each group, like so:
a
b
c
ab
ac
Part 1 asks for the sum of unique answers in each group. This is the set union of each group. Python sets have a handy union operator:
p1 = 0
for group in groups:
c = set()
for person in group:
c |= set(person)
p1 += len(c)
Part 2 asks for the sum of answers given by all members of each group. This is the set intersection, and Python has an operator for that, too!
p2 = 0
for group in groups:
c = set(string.ascii_lowercase)
for person in group:
c &= set(person)
p2 += len(c)