-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06customs.py
42 lines (30 loc) · 1.42 KB
/
day06customs.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
from functools import reduce
def group_answer_sets(raw_group):
"""
Returns a list of sets of the questions every person in `raw_group` answered
yes to.
"""
individual_answers = map(set, raw_group.split())
# Set intersection of all rows (individual answer sets)
return reduce(set.intersection, individual_answers)
if __name__ == "__main__":
with open("input/day06.txt", "r") as f:
# Split on empty line (2 newlines)
raw_input = f.read().split("\n\n")
# Part 1 (sum of "yes" answers)
# Remove newlines and convert strings to sets
question_sets = map(
lambda questions: set(filter(lambda c: not c.isspace(), questions)),
raw_input)
yes_answers = reduce(lambda acc, qset: acc + len(qset), question_sets, 0)
print(f"Part 1 result (sum of yes answers): {yes_answers}")
assert yes_answers == 7120, f"Unexpected part 1 answer: {yes_answers}"
# Part 2 (sum of group-wide "yes" answers)
# Find common answers within each group (delineated by "\n\n")
common_group_answers = map(group_answer_sets, raw_input)
# Sum of all groups' "yes" answer amounts
common_yes_answers = reduce(lambda acc, qset: acc + len(qset),
common_group_answers, 0)
print(
f"Part 2 result (sum of group-wide yes answers: {common_yes_answers}")
assert common_yes_answers == 3570, f"Unexpected part 2 answer: {common_yes_answers}"