Day 21
This commit is contained in:
25
day21/part1.py
Normal file
25
day21/part1.py
Normal 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))
|
||||
|
||||
Reference in New Issue
Block a user