This commit is contained in:
Sebastian Seedorf
2020-12-29 15:38:41 +01:00
parent aef62199e6
commit a04cbc39af
6 changed files with 278 additions and 2 deletions

25
day21/part1.py Normal file
View File

@@ -0,0 +1,25 @@
#!/usr/bin/env python3
import re
from collections import defaultdict
lines = (x.strip() for x in open("input.txt"))
REGEX = re.compile(r"((?:[a-z]+ )+)\(contains (.*)\)")
possible_ingredients = dict()
count = defaultdict(int)
for line in lines:
ingredients, allergens = REGEX.match(line).groups()
ingredients = set(ingredients.strip().split())
for ing in ingredients:
count[ing] += 1
allergens = allergens.strip().split(", ")
for allergen in allergens:
if allergen in possible_ingredients:
possible_ingredients[allergen].intersection_update(ingredients)
else:
possible_ingredients[allergen] = set(ingredients)
valids = set.union(*possible_ingredients.values())
print(sum(v for k, v in count.items() if k not in valids))