<jupyter_start><jupyter_text>Day 21: Allergen AssessmentYou reach the train's last stop and the closest you can get to your vacation island without getting wet. There aren't even any boats here, but nothing can stop you now: you build a raft. You just need a few days' worth of food for your journey.You don't speak the local language, so you can't read any ingredients lists. However, sometimes, allergens are listed in a language you do understand. You should be able to use this information to determine which ingredient contains which allergen and work out which foods are safe to take with you on your trip.You start by compiling a list of foods (your puzzle input), one food per line. Each line includes that food's ingredients list followed by some or all of the allergens the food contains.Each allergen is found in exactly one ingredient. Each ingredient contains zero or one allergen. **Allergens aren't always marked**; when they're listed (as in `(contains nuts, shellfish)` after an ingredients list), the ingredient that contains each listed allergen will be **somewhere in the corresponding ingredients list**. However, even if an allergen isn't listed, the ingredient that contains that allergen could still be present: maybe they forgot to label it, or maybe it was labeled in a language you don't know.For example, consider the following list of foods:    mxmxvkd kfcds sqjhc nhms (contains dairy, fish)    trh fvjkl sbzzf mxmxvkd (contains dairy)    sqjhc fvjkl (contains soy)    sqjhc mxmxvkd sbzzf (contains fish)The first food in the list has four ingredients (written in a language you don't understand): `mxmxvkd`, `kfcds`, `sqjhc`, and `nhms`. While the food might contain other allergens, a few allergens the food definitely contains are listed afterward: `dairy` and `fish`.The first step is to determine which ingredients **can't possibly** contain any of the allergens in any food in your list. In the above example, none of the ingredients `kfcds`, `nhms`, `sbzzf`, or `trh` can contain an allergen. Counting the number of times any of these ingredients appear in any ingredients list produces **`5`**: they all appear once each except `sbzzf`, which appears twice.Determine which ingredients cannot possibly contain any of the allergens in your list. **How many times do any of those ingredients appear?**Your puzzle answer was `1930`.<jupyter_code>input = """mxmxvkd kfcds sqjhc nhms (contains dairy, fish)
trh fvjkl sbzzf mxmxvkd (contains dairy)
sqjhc fvjkl (contains soy)
sqjhc mxmxvkd sbzzf (contains fish)""".split('\n')
input = open('day.21.input.txt', 'r').read().split('\n')
from functools import reduce
import re

def parse(s):
    i, a = re.search('(.*) [(]contains (.*)[)]', s).groups()
    i = set(i.split(' '))
    a = set(a.split(', '))
    return [i, a]

foods = [parse(i) for i in input]
ingredients = reduce(set.union, [{a for a in f[0]} for f in foods])
allergens = reduce(set.union, [{a for a in f[1]} for f in foods])

candidates = {a: reduce(set.intersection, [f[0] for f in foods if a in f[1]]) for a in allergens}

excluded = ingredients - reduce(set.union, candidates.values())
sum([len({a for a in f[0] if a in excluded}) for f in foods])<jupyter_output><empty_output><jupyter_text>Part TwoNow that you've isolated the inert ingredients, you should have enough information to figure out which ingredient contains which allergen.In the above example:* `mxmxvkd` contains `dairy`.* `sqjhc` contains `fish`.* `fvjkl` contains `soy`.Arrange the ingredients **alphabetically by their allergen** and separate them by commas to produce your **canonical dangerous ingredient list**. (There should **not be any spaces** in your canonical dangerous ingredient list.) In the above example, this would be `mxmxvkd,sqjhc,fvjkl`.Time to stock your raft with supplies. **What is your canonical dangerous ingredient list?**Your puzzle answer was `spcqmzfg,rpf,dzqlq,pflk,bltrbvz,xbdh,spql,bltzkxx`.<jupyter_code>while max([len(v) for v in candidates.values()]) > 1:
    known = {list(fs)[0] for a, fs in candidates.items() if len(fs) == 1}
    candidates = {a: fs - known if len(fs) > 1 else fs for a, fs in candidates.items()}
    
dangerous = [(k, list(v)[0]) for k, v in candidates.items()]
print(','.join([d[1] for d in sorted(dangerous, key=lambda d: d[0])]))<jupyter_output>spcqmzfg,rpf,dzqlq,pflk,bltrbvz,xbdh,spql,bltzkxx
{'fraction_non_alphanumeric': 0.09816933638443937, 'fraction_numerical': 0.004805491990846682, 'mean_word_length': 4.995884773662551, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '639677', 'license': 'MIT', 'n_tokens_mistral': 1332, 'n_tokens_neox': 1271, 'n_words': 610, 'path': 'day.21.ipynb', 'repo_name': 'markusschanta/adventofcode2020', 'size': '4370'}