Day 8Soup-Jar-Formulator 8000

penned by John Williams

After yet another early wake up from Patch, you're on the way to the North Pole Red Cross division - just as the chaos begins. This season, the elves are stretched to the limit. Floods, blizzards, and conflicts have left villages across the world in desperate need of food. Normally, you'd see elves carefully packing soup jars with the precise ingredients required to sustain those in need, but Patch soon explains that the Soup-Jar-Formulator 8000 - their secret weapon for perfectly layered jars - was critically damaged in the recent hacking attack. Ingredients that were once measured and stacked with magical precision are now coming out scrambled. Many jars are completely ruined, some would be inedible and some are entirely empty. Every jar is of utmost importance - can you find a temporary solution to allow the elves to continue to dispatch jars?


The Soup-Jar-Formulator 8000 normally follows a very intricate process to construct soup jars - but the attack has caused the intricate, precise layering process to fail, and jars are coming out with unpredictable and often entirely incorrect ingredients.

Thankfully, in the interest of time, the elves have agreed to accept jars containing approximately correct quantities of the required ingredients in any arrangement - so all you need to do is figure out how much of each ingredient the machine would have output into each jar, allowing the elves to scale up your solution.

Each jar contains a stack of layers. Pouring removes ingredients from the top of the stack in the jar. Blending does not change the total quantity but merges all layers into a single, inseparable layer preserving proportions.

Soup-Jar-Formulator 8000 commands:

  • ADD <quantity> <ingredient> TO <jar_number>
    • Adds a new layer to the top of the stack in jar_number.
    • The new layer contains of quantity units of a single ingredient ingredient.
  • POUR <quantity> <jar_number_from> INTO <jar_number_to>
    • Removes layers from the top of jar_number_from totaling up to quantity.
    • If the top layer is smaller than the requested quantity, the remainder is removed from the next layer(s). The final layer might not be entirely removed.
    • Layers are added to the top of jar_number_to in reverse order (so the last removed layer becomes the topmost in jar_number_to).
  • BLEND <jar_number>
    • Combines all layers in jar_number into a single layer.
    • The quantity of this layer should be the sum of the quantities of the ingredients it consists of.
    • Ingredient quantities within this layer should remain unchanged, so when poured, a proportional amount of each ingredient comes out.
  • EMPTY <quantity> <jar_name>
    • Removes quantity units from the top layer of jar_name.
    • Effectively the same as pouring, but doesn't add the removed layers to any other jar.

Given a sequence of commands, can you output the quantities of each ingredient in each jar after executing all commands?

Output format:

Give a string of semicolon separated repesentations of each jar, in ascending order (i.e. jar 0, then jar 1, then jar 2, ...). Represent each jar as a string of ingredient-quantity pairs ingredient_name quantity, separated by commas, sorted by ingredient name in alphabetical order. Round each total quantity up to the nearest integer. Ignore layers in this output - each ingredient should only be specified once, with its total quantity across all layers in the jar. Note that spaces are not required and the answer will be marked ignoring spacing.

Example scenario and output:

jar 0:
+----------+
| apple 30 |
+----------+

jar 1:
+-----------------------+
| cinnamon 20.3         |
+-----------------------+
| apple 10, banana 21.7 |
+-----------------------+
| banana 18.7           |
+-----------------------+

Output:

apple 30 ; apple 10,banana 41,cinnamon 21
  • Here, the first jar contains only 30 units of apple.
  • The second jar contains apple, banana, and cinnamon. The exact layer composition cannot be known in this case. Note that the ingredients are sorted alphabetically by ingredient name (cinnamon > banana > apple).
    • Banana 40 is calculated by 21.7 + 18.7 = 40.4 (round up to 41)

Example input

ADD 50 sugar TO 0
ADD 30 salt TO 0
POUR 40 0 INTO 1
BLEND 1
EMPTY 10 0
POUR 20 1 INTO 0

Step 1: ADD 50 sugar TO 0

A new layer of 50 units of sugar is added to the top of jar 0.

jar 0:
+----------+
| sugar 50 |
+----------+

Step 2: ADD 30 salt TO 0

A new layer of 30 units of salt is added on top of jar 0.

jar 0:
+----------+
| salt 30  |  <-- top
+----------+
| sugar 50 |
+----------+

Step 3: POUR 40 0 INTO 1

Remove 40 units from the top of jar 0:

  • The top layer is fully removed, and the remaining 10 units are taken from the 50 sugar layer
  • Layers removed (top-first): [30 salt, 10 sugar]
  • Add these to jar 1 in reverse order - jar 1 top becomes 30 salt, then 10 sugar beneath it.
jar 0:
+----------+
| sugar 40 |  <-- remaining after 10 removed
+----------+

jar 1:
+----------+
| salt 30  |  <-- top
+----------+
| sugar 10 |
+----------+

Step 4: BLEND 1

Merge all layers in jar 1 into a single layer, preserving total quantity and ingredient proportions:

  • Total quantity: 10 + 30 = 40
  • Ingredients:
    • Sugar: 10
    • Salt: 30
jar 0:
+----------+
| sugar 40 |
+----------+

jar 1:
+-------------------+
| sugar 10, salt 30 |  <-- blended layer
+-------------------+

Step 5: EMPTY 10 0

Remove 10 units from the top of jar 0:

jar 0:
+----------+
| sugar 30 |
+----------+

jar2:
+---------------------+
| sugar 10, salt 30   |
+---------------------+

Step 6: POUR 20 1 INTO 0

Pour 20 units (exactly half) of the combined blended layer from jar 1 in to jar 0:

jar 0:
+------------------+
| sugar 5, salt 15 |
+------------------+
| sugar 30         |
+------------------+

jar 1:
+---------------------+
| sugar 5, salt 15    |
+---------------------+

Output: salt 15,sugar 35 ; salt 15,sugar 5

[Back to 2025]