Module aoc::year2020::day21

source ·
Expand description

§Allergen Assessment

The rules can be expressed as:

  • If an ingredient is on a line, then it may contain the listed allergens.
  • If an ingredient is not on a line, then it definitely does not contain the listed allergens, as some other food on the line must instead contain the allergen.

§Part One

To find the safe foods we build two sets, then subtract them to find out the remaining possible allergens. It’s important to only subtract the sets at the very end in order to prevent re-adding a previously excluded allergen. Using kfcds from the example:

LinePossibleImpossible
1Dairy, FishØ
2Dairy, FishDairy
3Dairy, FishDairy, Soy
4Dairy, FishDairy, Soy, Fish

Final result: Ø (the empty set)

§Part Two

This is a constraint satisfaction problem, similar to day 16. Using fvjkl from the example:

LinePossibleImpossible
1ØDairy, Fish
2DairyDairy, Fish
3Dairy, SoyDairy, Fish
4Dairy, SoyDairy, Fish

Final result: Soy

To solve there must be at least one ingredient with only one allergen remaining. As this allergen can only belong to this ingredient, we eliminate it from other ingredients. This causes a chain reaction where a second ingredient will reduce to only one allergen, continuing until all allergens have been resolved.

As there are less than 64 lines and allergens we can speed things up by using bitwise logic on a usize to compute set addition and subtraction. To add to a set use OR |, to remove use AND & and to calculate the size use count_ones.

Structs§

Functions§