--- Day 8: Soup-Jar-Formulator 8000 ---
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>
jar_number.quantity units of a single ingredient ingredient.POUR <quantity> <jar_number_from> INTO <jar_number_to>
jar_number_from totaling up to quantity.quantity, the remainder is removed from the next layer(s). The final layer might not be entirely removed.jar_number_to in reverse order (so the last removed layer becomes the topmost in jar_number_to).BLEND <jar_number>
jar_number into a single layer.quantity of this layer should be the sum of the quantities of the ingredients it consists of.EMPTY <quantity> <jar_name>
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
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:
[30 salt, 10 sugar]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:
10 + 30 = 40jar 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