-
Notifications
You must be signed in to change notification settings - Fork 0
/
21.py
54 lines (42 loc) · 1.49 KB
/
21.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
lines = [x.strip() for x in open('21.test', 'r').readlines()]
lines = [x.strip() for x in open('21.input', 'r').readlines()]
allergenes = {}
all_ingredients = set()
for line in lines:
allergene_list = "".join(line.split('(contains ')[1])[:-1].split(', ')
ingredient_list = set("".join(line.split('(')[0])[:-1].split(' '))
all_ingredients = all_ingredients.union(ingredient_list)
for a in allergene_list:
if a not in allergenes:
allergenes[a] = set(ingredient_list)
else:
allergenes[a] = allergenes[a].intersection(set(ingredient_list))
for a in allergenes:
print a, allergenes[a]
count = 0
for line in lines:
ingredient_list = set("".join(line.split('(')[0])[:-1].split(' '))
for i in ingredient_list:
no_allergene = True
for a in allergenes:
if i in allergenes[a]:
no_allergene = False
break
if no_allergene:
count += 1
print "Solution part 1: %d" % count
while True:
unique = []
for a in allergenes:
if len(allergenes[a]) == 1:
unique.append(list(allergenes[a])[0])
if len(unique) == len(allergenes):
break
for a in allergenes:
if len(allergenes[a]) > 1:
allergenes[a] = allergenes[a].difference(unique)
allergenes = [(a, list(allergenes[a])[0]) for a in allergenes]
allergenes.sort()
allergenes = [a[1] for a in allergenes]
result = ",".join(allergenes)
print "Solution part 2: %s" % result