06.py 1.26 KB
Newer Older
Simon Marquardt (smrqdt)'s avatar
day 06  
Simon Marquardt (smrqdt) committed
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
48
49
#!/usr/bin/env python3


def main():
    answers = parse_input("06-input.txt")
    count_union = count_unified(answers)
    print(f"The count of unified group answers with yes is {count_union}.")
    count_intersect = count_same(answers)
    print(f"The count of unified group answers with yes is {count_intersect}.")


def count_same(answers: tuple[tuple[str]]):
    sum = 0
    for group in answers:
        sum += len(intersection_group_answers(group))
    return sum


def intersection_group_answers(group_answers: tuple[str]):
    intersection = set("abcdefghijklmnopqrstuvwxyz")
    for person in group_answers:
        intersection.intersection_update(person)
    return intersection


def count_unified(answers: tuple[tuple[str]]):
    sum = 0
    for group in answers:
        sum += len(unify_group_answers(group))
    return sum

def unify_group_answers(group_answers: tuple[str]):
    unified = set()
    for person in group_answers:
        unified.update(person)
    return unified


def parse_input(filename: str) -> tuple[tuple[str]]:
    answers = []
    with open(filename, "r") as file:
        groups = file.read().split("\n\n")
        for group in groups:
            answers.append(group.splitlines())
    return tuple(answers)


if __name__ == "__main__":
    main()