Skip to content

Latest commit

 

History

History

Day 06

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

🎄 Advent of Code 2020: Day 6 🎄

Read the statement before watching the solution.

Get your puzzle input.

Part 1

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
Execution time: 2ms

Part 2

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
Execution time: 3ms