-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_custom_declarations.py
118 lines (90 loc) · 3.01 KB
/
process_custom_declarations.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python
"""
Advent of Code 2020 - Day 6: Custom Customs
"""
import sys
from pathlib import Path
import bisect
from typing import Iterator, List
INPUT_FILES = ['./short-input.txt', './input.txt']
def split_groups(file: Path) -> Iterator[str]:
"""
Split input file into groups
:param file: custom declaration file to process
:return: generator yielding a list of fields
"""
group_answers = ''
for line in open(file):
if line == '\n':
yield group_answers
group_answers = ''
continue
group_answers += line.strip()
yield group_answers
def process_custom_declarations(file: Path, verbose: bool) -> int:
"""
Process a given custom declaration file
:param file: custom declaration file to process
:param verbose: print extra information
:return: sum of yes answers
"""
sum_yes_answers = 0
for group_answers in split_groups(file=file):
unique_answers = ''.join(set(group_answers))
if verbose:
print(f'group answers: {group_answers}, unique answers {unique_answers}')
sum_yes_answers += len(unique_answers)
return sum_yes_answers
def split_groups_part2(file: Path) -> Iterator[List]:
"""
Split input file into groups
:param file: custom declaration file to process
:return: generator yielding a list of fields
"""
group_answers = list()
for line in open(file):
if line == '\n':
yield group_answers
group_answers = list()
continue
group_answers.append(line.strip())
yield group_answers
def process_custom_declarations_part2(file: Path, verbose: bool) -> int:
"""
Process a given custom declaration file
:param file: custom declaration file to process
:param verbose: print extra information
:return: sum of yes answers
"""
sum_all_yes_answers = 0
for group_answers in split_groups_part2(file=file):
if len(group_answers) == 1:
all_yes_answers = len(group_answers[0])
else:
all_yes_answers = sum(1 for c in group_answers[0] if all(c in answer for answer in group_answers[1:]))
if verbose:
print(f'group answers: {group_answers}, unique answers {all_yes_answers}')
sum_all_yes_answers += all_yes_answers
return sum_all_yes_answers
def main() -> int:
"""
Main function
:return: Shell exit code
"""
for file in INPUT_FILES:
verbose = 'short' in file
sum_yes_answers = process_custom_declarations(
file=Path(file), verbose=verbose)
print(f'In file {file}, sum of yes answers: {sum_yes_answers}')
print('Part 2')
for file in INPUT_FILES:
verbose = 'short' in file
sum_all_yes_answers = process_custom_declarations_part2(
file=Path(file), verbose=verbose)
print(f'sum all yes answers: {sum_all_yes_answers}')
return 0
if __name__ == '__main__':
"""
Command line entry-point
"""
sys.exit(main())