10
votes
Day 21: Allergen Assessment
Today's problem description: https://adventofcode.com/2020/day/21
Join the Tildes private leaderboard! You can do that on this page, by entering join code 730956-de85ce0c
.
Please post your solutions in your own top-level comment. Here's a template you can copy-paste into your comment to format it nicely, with the code collapsed by default inside an expandable section with syntax highlighting (you can replace python
with any of the "short names" listed in this page of supported languages):
<details>
<summary>Part 1</summary>
```python
Your code here.
```
</details>
Still not done with Day 20 Part 2 (something is bugged with my "does this tile need to be flipped to be inserted" detection), but it was nice to get another weekday puzzle.
C#
Part 1
Part 2
Helper
Commentary
I swear, the word "exactly" got added to the puzzle description (in "Each allergen is found in exactly one ingredient") when I refreshed the page. I was staring at the example and couldn't figure out a damn thing to do with it, but then I refreshed and re-read and it became clear. I basically started trying to solve (what I was guessing) was Part 2, and then hand to argue myself into the fact that all the ingredients not in the possible-allergens list must be impossible. I'm still not 100% convinced, but I couldn't find a counterexample by hand, at least.
Part 2 was just going through the possibles, finding a unique solution, and subtracting it off from all the others. I almost feel like this step is worth generalizing at this point.
The holidays are getting busier, so I think I'm going to bow out for the rest of the year. I might come back later, but I'm not going to try to keep up with these day-of.
It's been fun though, and I hope everyone learned something along the way! Best of luck to those of your sticking with it.
welp, I did this last night in Sheets. It was fairly straightforward, but more formulas than I wanted to use. I could probably crush it down to nine formulas, but it wouldn't be any prettier. :)
Parts 1 & 2
Python
Wait, this one was easy after all? I expected so much horrible from part 2. Ultimately, it was all about figuring out that "could contain allergen" means that a food name is present in every list that has that allergen. The food names for which this isn't true for any allergen can't have any allergens. Part 2 was just cleaning up by marking the already fixed ones (food names with a single possible allergen) until all are assigned.
Part 1
Rust
This one wasn't too bad, but I did find it hard to keep track of what I was doing. I kept forgetting whether variables called
allergen
in my code referred to ingredients that were allergens (e.g. "kxjttzg"), or the final allergen (e.g. "dairy"). I have variables calledallergen_possibilities
andpossible_allergens
each containing very different things. I really should go through and rename everything.Rust
Python
Repo Link
Part 1
To solve this one, I realized you could use set operations to determine which ingredients mapped to which allergens. For instance, anytime you get a listing for say "dairy", you perform the intersection of the current set of possible incredients with the new set. This way, you only get the ingredients in common with the two ingredient lists. At the end of processing all the input, you will then have a table where for each allergen, you will have a set of possible ingredients that map to them.
My main hangups on this were as follows:
I started by counting each ingredient instead of just using sets. I realized after viewing the table of counts I created that I could just store the items that appear multiple times and work for there.
My first solution actually (unknowingly) tried to solve Part 2 by figuring out the exact mapping of dangerous ingredient to allergen and this lead me down the wrong path as I got the wrong answer for Part 1. Because the website said my answer was too low, I figured I was pruning too much.
Once I focused only on solving Part 1, I ran into an infuriating possible Python bug or quirk. For some reason, performing
a &= b
produced different results froma = a & b
. I made aWTF
note in my code to mark where this ocurred. This cost me about an hour as I couldn't figure out why my result was too high (wasn't eliminating enough).Part 2 (diff)
Once I got Part 1 successfully completed, then Part 2 was straightforward since I had mistakenly done most of the logic while working on Part 1. The idea is to look for any allergen mappings with only one possible candidate. If you have such a mapping, then that is the dangerous ingredient/allergen combo. Take that ingredient, and remove it as a candidate from all the other allergens and repeat this process until there are no more mappings with only one possible candidate.