aboutsummaryrefslogblamecommitdiffstats
path: root/2020/day06/custom-customs.py
blob: d16e71b37d2106539766264561db0eb8d2af1664 (plain) (tree)














































                                                                
from typing import Generator

EXAMPLE = """   abc

a
b
c

ab
ac

a
a
a
a

b
"""

def unique(raw: str) -> Generator[set, None, None]:
    for group in raw.split('\n\n'):
        yield set(group.strip().replace('\n', ''))

assert sum((len(g) for g in unique(EXAMPLE))) == 11

with open('data/answers.txt') as f:
    print(sum((len(g) for g in unique(f.read()))))

# Part Two

def everyone(raw: str) -> int:
    total = 0
    for chunk in raw.split('\n\n'):
        group = chunk.strip().split('\n')
        counts = {}

        for per in group:
            for q in per:
                counts[q] = counts.get(q, 0) + 1
        total += sum((len(group) == x for x in counts.values()))
    return total

assert everyone(EXAMPLE) == 6

with open('data/answers.txt') as f:
    print(everyone(f.read()))