-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC2020_06.py
executable file
·71 lines (51 loc) · 1.39 KB
/
AoC2020_06.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#! /usr/bin/env python3
#
# Advent of Code 2020 Day 6
#
from aoc import my_aocd
def _sum_of_counts(lists: list[list]) -> int:
return sum([len(lst) for lst in lists])
def part_1(inputs: tuple[str]) -> int:
blocks = my_aocd.to_blocks(inputs)
unique_anwers_per_group = []
for block in blocks:
unique_answers_for_group = set()
for input_ in block:
unique_answers_for_group.update(input_)
unique_anwers_per_group.append(unique_answers_for_group)
return _sum_of_counts(unique_anwers_per_group)
alfabet = "abcdefghijklmnopqrstuvwxyz"
def part_2(inputs: tuple[str]) -> int:
blocks = my_aocd.to_blocks(inputs)
common_answers_per_group = []
for block in blocks:
common_answers_for_group = set(alfabet)
for input_ in block:
common_answers_for_group = common_answers_for_group\
.intersection(set(input_))
common_answers_per_group.append(common_answers_for_group)
return _sum_of_counts(common_answers_per_group)
TEST = """\
abc
a
b
c
ab
ac
a
a
a
a
b
""".splitlines()
def main() -> None:
my_aocd.print_header(2020, 6)
assert part_1(TEST) == 11
assert part_2(TEST) == 6
inputs = my_aocd.get_input(2020, 6, 2042)
result1 = part_1(inputs)
print(f"Part 1: {result1}")
result2 = part_2(inputs)
print(f"Part 2: {result2}")
if __name__ == '__main__':
main()