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

37 lines
1.1 KiB
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())
done = dict()
while len(possible_ingredients):
for allergen, ingredients in possible_ingredients.copy().items():
if len(ingredients) == 1:
ing = ingredients.pop()
done[allergen] = ing
possible_ingredients.pop(allergen)
for ings in possible_ingredients.values():
ings.discard(ing)
print(",".join(done[x] for x in sorted(done.keys())))