-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
42 lines (33 loc) · 1.19 KB
/
main.py
File metadata and controls
42 lines (33 loc) · 1.19 KB
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 collections import defaultdict
import numpy as np
from more_itertools import flatten
with open("input.txt") as f:
lines = [x.strip() for x in f]
foods = [x.split(" (contains ")[0].split() for x in lines]
allergens = [x.split(" (contains ")[1][:-1].split(", ") for x in lines]
recipe = list(zip(foods, allergens))
poss_allergens = sorted(set(list(flatten(allergens))))
poss = []
for _, pa in enumerate(poss_allergens):
k = []
for fs, allergs in recipe:
if pa in allergs:
k.append(set(fs))
poss.append(k)
reduced = [set.intersection(*ps) for ps in poss]
remaining_foods = sorted(set(flatten(reduced)))
count = len([f for f in flatten(foods) if f not in remaining_foods])
print(count)
poss_mat = np.array([[1 if r in red else 0 for r in remaining_foods] for red in reduced])
change = True
seen = set()
while change:
change = False
for i, row in enumerate(poss_mat):
if sum(row) == 1 and i not in seen:
seen.add(i)
change = True
col = np.argmax(row)
poss_mat[:, col] = 0
poss_mat[i, col] = 1
print(",".join(remaining_foods[np.argmax(poss_mat[i])] for i in range(len(remaining_foods))))