Files
python-aoc-2020/day21/part1.py
Sebastian Seedorf a04cbc39af Day 21
2020-12-29 15:38:41 +01:00

26 lines
793 B
Python

#!/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))