by @npanuhin
Read the statement before watching the solution.
In Part 1, we were asked to count the number of letters (questions) contained in at least one of given lines (people). This is easy to implement using a set: let's call the union
method for each line or its alias - bitwise |
(|=
) operator.
The sum of these counts for each group of people will be the answer.
data = [set()]
with open("input.txt") as file:
for line in file:
if not line.strip():
data.append(set())
else:
data[-1] |= set(line.strip())
print(sum(len(group) for group in data))
6506
In Part 2, we were asked to count the same thing, except that now every line must contain the given letter. Thus, we can change the union
method to intersection
(&
operator):
from string import ascii_letters
data = [set(ascii_letters)]
with open("input.txt") as file:
for line in file:
if not line.strip():
data.append(set(ascii_letters))
else:
data[-1] &= set(line.strip())
print(sum(len(group) for group in data))
3243