-
Notifications
You must be signed in to change notification settings - Fork 0
/
day21.py
77 lines (67 loc) · 2.79 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
inp_f = open("input/day21.txt", "r")
inp = inp_f.read()
inp_list = [line for line in inp.split("\n") if line != ""]
def part1():
allergen_to_ingredients = {}
all_ingredients = []
ingredient_lines = []
for line in inp_list:
split = line.split(" (contains ")
ingredients = split[0].split(" ")
allergens = split[1][:-1].split(", ")
for allergen in allergens:
if allergen not in allergen_to_ingredients:
allergen_to_ingredients[allergen] = []
allergen_to_ingredients[allergen].append(set(ingredients))
for ingredient in ingredients:
if ingredient not in all_ingredients:
all_ingredients.append(ingredient)
ingredient_lines.append(ingredients)
allergen_intersection = {}
possible_ingredients = []
impossible_ingredients = []
for allergen in allergen_to_ingredients:
allergen_intersection[allergen] = set.intersection(*allergen_to_ingredients[allergen])
for ingredient in allergen_intersection[allergen]:
if ingredient not in possible_ingredients:
possible_ingredients.append(ingredient)
for ingredient in all_ingredients:
if ingredient not in possible_ingredients:
impossible_ingredients.append(ingredient)
count = 0
for ingredient in impossible_ingredients:
for ingredients in ingredient_lines:
if ingredient in ingredients:
count += 1
return count
def part2():
allergen_to_ingredients = {}
for line in inp_list:
split = line.split(" (contains ")
ingredients = split[0].split(" ")
allergens = split[1][:-1].split(", ")
for allergen in allergens:
if allergen not in allergen_to_ingredients:
allergen_to_ingredients[allergen] = []
allergen_to_ingredients[allergen].append(set(ingredients))
allergen_intersection = []
for allergen in allergen_to_ingredients:
allergen_intersection.append((allergen, set.intersection(*allergen_to_ingredients[allergen])))
allergen_intersection.sort(key=lambda item: len(item[1]))
res_all = []
while len(res_all) < len(allergen_intersection):
for allergen, ingredients in allergen_intersection:
ingredients = ingredients.difference(set([combo[1] for combo in res_all]))
if len(ingredients) == 1:
ingredient = list(ingredients)[0]
if (allergen, ingredient) in res_all:
continue
res_all.append((allergen, ingredient))
else:
continue
res_all.sort(key=lambda item: item[0])
res = [combo[1] for combo in res_all]
return ','.join(res)
if __name__ == "__main__":
print(part1())
print(part2())