VP 31: Allergens (15 pts + 15 extra)

What You Need

Any computer with Python 3. You can also use an online Python environment, such as https://colab.research.google.com

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 nhms. While the food might contain other allergens, a few allergens the food definitely contains are listed afterward: dairy and fish. Therefore, one of those ingredients contains dairy and another of them contains fish. Also, no other ingredient contains either dairy or fish.

The second line lists four ingredients: trh fvjkl sbzzf mxmxvkd, and this line contains dairy. The only common ingredient, contaied in both lines 1 and 2, is mxmxvkd, so this is the ingredient that contains dairy.

One of the remaining ingredients in line 1, kfcds sqjhc nhms, must contain fish. Line 4 also contains fish. The only ingredient of those three possibilities that occurs in line 4 is sqjhc, so this ingredient must contain fish.

Line 3 contains only two ingredients: sqjhc fvjkl, and contains the allergen soy. We already know that sqjhc contains fish, so fvjkl must contain soy. (Remember that each ingredient can contain only 0 or 1 allergens.

Each allergen occurs in only one ingredient, and we've accounted for all of the three possible allergens, so all the other ingredients must contain 0 allergens.

Crossing out every ingredient containing an allergen, we get:

mxmxvkd kfcds sqjhc nhms (contains dairy, fish)
trh fvjkl sbzzf mxmxvkd (contains dairy)
sqjhc fvjkl (contains soy)
sqjhc mxmxvkd sbzzf (contains fish)
The remaining ingredients are allergen-free:
kfcds nhms trh sbzzf 
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?

VP 31.1: Most Common Allergen (5 pts)

Use this data:
https://samsclass.info/COMSC132/proj/VP31
Find the allergen that is found in the most foods.

That's the flag.

VP 31.2: Safe Ingredients (10 pts)

Use the same data as VP 31.1. Count the total number of occurrences of ingredients that contain no allergens.

That's the flag.

Dangerous Ingredients

Now that you've isolated the inert ingredients, you should have enough information to figure out which ingredient contains which allergen. In the above example: Arrange the ingredients alphabetically by their allergen and concatenate them to produce your canonical dangerous ingredient list.

In the above example, this would be:

mxmxvkdsqjhcfvjkl

VP 31.3: Dangerous Ingredients List (15 pts extra)

Use the same data as VP 31.1.

Construct the canonical dangerous ingredient list, as explained above.

That's the flag.

Source

Adapted from the Advent of Code.

Posted 9-7-24
Flag 26.3 updated 9-17-24
Video added 10-9-24