Advent of Code 2020: Day 21
, 8 min readadvent of code 2020
Part 1
You 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:
|
|
Determine which ingredients cannot possibly contain any of the allergens in your list. How many times do any of those ingredients appear?
You can read the full text here. I love this puzzle.
I initially started to try and solve the whole puzzle immediately, but my code got quite complex and yucky. So as always do when code is not happening, I got up and took a short break. After coming back with a fresh mind I completely change strategy.

Consider the 2 lines of the example:
|
|
they both contain an ingredient with dairy, thus we know that dairy could be any of the ingredients that are in both lists. This is just an intersection. After processing the first line we know that:
|
|
then for the second line we know that dairy is now the intersection of the current geuesses and the new clue,
|
|
thus we have,
|
|
We would now continue this for every line. Now, for each allergen we have a list of all the ingredients which could contain it. Great, but how we do write the code for this? Well the key thing we are doing is reading in each line of the data and storing it so we know the ingredients and potential allergens.
|
|
In the code above we open up a file, use regex to search for words (which don’t contain spaces or brackets), and then store each line of the input as pair of set
s. The first set is the ingredients and the second set
are the possible allergens. Cool. Now we need a way of taking sets and calculating insersections of the set
s. For this, I wrapped the set_intersection
function to make it more ergonomic for our use case,
|
|
Then we just loop through every set of ingredients in our input, take the intersection for each allergen as described.
|
|
If this is the first time we have seen the allergen then we just store every ingredient in this row. After we’ve applied this filter we can loop every ingredient in the input again and coount how many are not possible candidates for any allergen.
|
|
There we go, first ⭐!
Part 2
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 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?
You can read the full text here. So part 2 is fairly straight forward. Now that we have a short list of candidates for every allergen we just need to figure out which is which. For my input we can print out the possibilites for every allergen,
|
|
We can then solve this trivially with pen and paper. The eggs has to be caused by “nlxsmb”, thus the peanuts must be “ttxvphb” and then the fish must be “rnbhjk” and so on. In fact, this is how I did solve it, but for the interested reader I’ve attached code below which solves it more generally,
|
|
and there we go, both stars ⭐⭐!
Reflections
I really enjoyed todays puzzle, I thought it was a really good puzzle to solve. I think the code I’ve written is both very readable and quite concise. The algorithm is also quite efficient.
We start by looping through and reading in every value in the input. This is
O(N)
-time and space, where N
is the number of lines in the input. We then loop through each line taking the intersection for each of the possible allergens. This step scales similarly in both time and space. I’m also assuming my map
lookup is O(1)
, which is not currently true but can be achieved with an unordered_map
.
The second step just involves looping over each of the allergens a few times, thus it just scales as O(M)
where M
is the number of allergens, which is very small. I particularly liked that I could solve part 2 on paper in less than 30 seconds!
Anyway, thanks for reading. I’m back up to date with Advent of Code puzzles and due to covid I’m not travelling for Christmas. Look forward to seeing you all tomorrow, hopefully we can have a couple of slightly easier puzzles!
I have no affiliation with AoC. I’m just a fan of the programming puzzles. If you enjoy them too, please feel free to join in and support the creators