-
Notifications
You must be signed in to change notification settings - Fork 0
/
day21.py
46 lines (38 loc) · 1.36 KB
/
day21.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
import re
with open("input/day21", "r") as input:
data = input.read().split("\n")
data.pop()
allergens = {}
allIngredients = set()
foods = []
for entry in data:
m = re.match(r"(.+) \(contains (.+)\)", entry)
ingredient = set(m[1].split(" "))
foods.append(ingredient)
allIngredients = allIngredients.union(ingredient)
for allg in m[2].split(", "):
if allg not in allergens:
allergens[allg] = ingredient
else:
allergens[allg] = allergens[allg].intersection(ingredient)
nonAllergents = allIngredients.copy()
for key in allergens:
nonAllergents = nonAllergents.difference(allergens[key])
numberOfNonAllergents = sum([len(nonAllergents.intersection(f)) for f in foods])
# Part 2
usedSets = set()
while sum([len(allergens[key]) for key in allergens]) != len(allergens):
filterSet = set()
for key in allergens:
if len(allergens[key]) == 1 and key not in usedSets:
filterSet = allergens[key]
usedSets.add(key)
break
for key in allergens:
if allergens[key] == filterSet:
continue
allergens[key] = allergens[key].difference(filterSet)
allergensSorted = list(allergens.keys())
allergensSorted.sort()
print("Part 1: " + str(numberOfNonAllergents))
print("Part 2: " + str(",".join([list(allergens[key])[0] for key in allergensSorted])))