-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc21.py
69 lines (56 loc) · 1.85 KB
/
aoc21.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
62
63
64
65
66
67
68
69
import re
def get_input():
with open('input21.txt') as file:
return [x.strip() for x in file]
input = [
"mxmxvkd kfcds sqjhc nhms (contains dairy, fish)",
"trh fvjkl sbzzf mxmxvkd (contains dairy)",
"sqjhc fvjkl (contains soy)",
"sqjhc mxmxvkd sbzzf (contains fish)",
]
input = get_input()
all_allergens = set()
all_ingredients = set()
lines = []
for line in input:
m = re.fullmatch("(.*) \\(contains (.*)\\)", line)
if m is None:
print(f"Unable to parse '{line}'")
exit()
part1, part2 = m.groups()
ingredients = part1.split(" ")
for i in ingredients:
all_ingredients.add(i)
allergens = part2.split(", ")
for a in all_allergens:
all_allergens.add(a)
lines.append((ingredients, allergens))
possibilities = {}
for ingredients, allergens in lines:
for allergen in allergens:
if allergen in possibilities.keys():
possibilities[allergen].intersection_update(ingredients)
else:
possibilities[allergen] = set(ingredients)
unknown_ingredients = set(all_ingredients)
for s in possibilities.values():
unknown_ingredients.difference_update(s)
count = 0
for ingredients, allergens in lines:
count += sum([ingredients.count(x) for x in unknown_ingredients])
print(f"Number of ingredients that can't be any allergen: {count}")
result = {}
while True:
found = [x for x,y in possibilities.items() if len(y)==1]
if len(found)==0:
break
tmp = []
for f in found:
i = possibilities[f].pop()
result[f] = i
tmp.append(i)
del possibilities[f]
for p in possibilities:
possibilities[p].difference_update(tmp)
canonical_ingredient_list = ",".join([y for x,y in sorted([(x,y) for x, y in result.items()], key = lambda pair: pair[0])])
print(f"Canonical ingredient list: {canonical_ingredient_list}")