blob: 0cd31c5941f96b6fc7e24f08670ce4b7fa4b5113 (
plain)
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
|
import fileinput
import re
from collections import defaultdict
L = list([l.strip() for l in fileinput.input()])
FOODS = []
for l in L:
first, rest = l.split("(contains ")
I = set(first.split())
A = set(rest[:-1].split(", "))
FOODS.append((I, A))
all_I = set()
all_A = set()
for I, A in FOODS:
all_A |= A
all_I |= I
# in allergens -> corresponding ingredient in list
COULD_BE = {i: set(all_A) for i in all_I}
C = defaultdict(int)
for I, A in FOODS:
for i in I:
C[i] += 1
# for each allergen, all the missing ingredients are definitely not it
# this is the only thing you can deduce!
for a in A:
for i in all_I:
if i not in I:
COULD_BE[i].discard(a)
MAPPING = {}
USED = set()
p1 = 0
for i in all_I:
if not COULD_BE[i]:
p1 += C[i]
print(p1)
while len(MAPPING) < len(all_A):
for i in all_I:
poss = [a for a in COULD_BE[i] if a not in USED]
if len(poss) == 1:
MAPPING[i] = poss[0]
USED.add(poss[0])
sorted_i = ",".join([k for k, v in sorted(MAPPING.items(), key=lambda kv: kv[1])])
print(sorted_i)
|