aboutsummaryrefslogtreecommitdiffstats
path: root/2020/day06/custom-customs.py
blob: d16e71b37d2106539766264561db0eb8d2af1664 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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()))