AoC 2020 D21: Allergen Assessment
| Problem statement | Source code | Tags: Constraint satisfaction
← Previous Back to AoC Index Next →
Part 1
If an allergen is found in lists A, B, and C, then the ingredient must be in the intersection of those lists. So we can build a mapping from allergen to the intersection of all ingredient lists that mention that allergen.
Those ingredients that cannot possibly contain any allergen are the ingredients not in possible_ingredients.values().
Part 2
This is basically the same as Day 16, except now we can't solve the constraints in a fixed order, because the constraint sets are no longer nicely nested with increasing sizes. Consider this example:
Here, the constraints can only be solved in the order nuts → soy → shellfish, but because both shellfish and soy have constraint size 2, we cannot know in advance which to solve first. Since there are only 8 allergens anyway, I use a solution to pull one at a time: