-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21.py
49 lines (39 loc) · 1021 Bytes
/
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
from lib import *
input = read_input(2020, 21)
lines = input.splitlines()
allergens = {}
ing = set()
cnt = Counter()
for line in lines:
i, a = line.split(" (contains ")
i = i.split()
ing.update(i)
cnt.update(i)
for x in a.strip(")").split(", "):
if x not in allergens:
allergens[x] = set(i)
else:
allergens[x] &= set(i)
for k in allergens.values():
ing -= k
print(sum(cnt[x] for x in ing))
allergens = {}
for line in lines:
i, a = line.split(" (contains ")
i = i.split()
for x in a.strip(")").split(", "):
if x not in allergens:
allergens[x] = set(i)
else:
allergens[x] &= set(i)
found = {}
while allergens:
for k, v in [*allergens.items()]:
if len(v) != 1:
continue
(found[k],) = v
for t in allergens.values():
if found[k] in t:
t.remove(found[k])
allergens.pop(k)
print(",".join(b for a, b in sorted(found.items())))