-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_6.py
61 lines (46 loc) · 1.52 KB
/
day_6.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
"""
1.
For each group, count the number of questions to which anyone answered "yes". What is the sum of those counts?
2.
As you finish the last group's customs declaration, you notice that you misread one word in the instructions:
You don't need to identify the questions to which anyone answered "yes"; you need to identify the questions
to which everyone answered "yes"!
"""
filename = 'data/input_6.txt'
def read_groups():
with open(filename) as fp:
lines = [line.strip("\n") for line in fp]
groups = []
group = []
for line in lines:
if line == "":
groups.append(group)
group = []
else:
group.append(line)
groups.append(group)
return groups
def get_group_answers_union(group):
union = set()
for answer in group:
union = union | set(list(answer))
return union
def part1():
groups = read_groups()
yes_answers_union = [get_group_answers_union(g) for g in groups]
ns = [len(y) for y in yes_answers_union]
print("Answer: %d" % sum(ns))
def get_group_answers_intersection(group):
intersection = set(list(group[0]))
for answer in group[1:]:
intersection = intersection & set(list(answer))
return intersection
def part2():
groups = read_groups()
yes_answers_intersection = [get_group_answers_intersection(g) for g in groups]
ns = [len(y) for y in yes_answers_intersection]
print("Answer: %d" % sum(ns))
if __name__ == '__main__':
part1()
print("******************")
part2()