Troubled Witch

Part 1 - Inventory of the Hut

The Witch’s Grimoire of Shadows has been torn apart, scattering its pages. Myregietha Frost possesses crates of ingredients, bundles of candles, piles of tools, and ritual diagrams—but she doesn't know how to combine them back into spells. Your task is to help her restore the book by organizing her inventory.

Myregietha needs your assistance! She has a list of all items in her witches hut, but it’s currently disorganized. The item details are provided in the input.txt file, formatted as (item type):(item).

To help Myregietha calculate her inventory coefficient, you need to determine the quantity of each item type and multiply them together.

For Example:
Input:
candle:candle_green
candle:candle_red
tool:mortar
ingredient:spider_leg
diagram:bone_script

Solution: 2 (candles) * 1 (tool) * 1 (ingredient) * 1 (diagram) = 2

Your solution is the product of the quantities of each of the four item types: candles, tools, ingredients, and diagrams.

Part 2 - Spell Combinations

With her ingredients, candles, tools, and diagrams gathered, the Witch is ready for her first true experiment. However, her spellbook remains torn—she cannot simply mix components haphazardly. Only unique combinations will awaken the latent magic. She traces each component with her wand, noting the distinct symbols, letters, and runes inscribed upon them.

Only the unique letters within a combination carry magical energy; duplicates fade into nothingness. For each ritual, she carefully selects:

Myregietha needs your help to determine the power of each possible spell so she can select the most potent ones for her restored spellbook. She measures a ritual's power by summing the ASCII values of all unique characters within that combination. By performing this calculation for every possible unique combination, she can assess the total magical potential remaining in her scattered Grimoire.

For Example:
Given Combination: candle_white, pentagram, pumpkin_seed, witch_hat
1. Unique Characters among all types: {'a', 'm', 'w', 'h', 'i', 'n', 'k', 'r', 'e', 'd', 'c', 'l', 's', 'g', '_', 'p', 't', 'u'}
2. ASCII Sum of the unique characters: 97 + 109 + 119 + 104 + 105 + 110 + 107 + 114 + 101 + 100 + 99 + 108 + 115 + 103 + 95 + 112 + 116 + 117 = 1931
Power Score: 1931

Your solution is the sum of the power scores for all possible unique combinations of (1 Ingredient, 1 Tool, 1 Diagram, 1 Candle).

Part 3 - The Oscillating Ritual

The Witch has meticulously measured the magical potential of every unique combination of ingredients, tools, candles, and diagrams. Yet, even with this raw power calculated, her Grimoire remains fragmented and unstable.

She understands that magic isn't solely about strength—it flows in oscillations, pulses, and counterbalances. To bring harmony to her spells, she must carefully order the energies.

Laying out all the power scores discovered in Part 2, she studies their magnitudes and follows the ancient ritual of the Oscillating Binding:
1. Sorts all the power scores in ascending order.
2. Alternates subtraction and addition down the list, starting with subtraction: first number minus second, plus third, minus fourth, and so on.

This process simulates the flow of magical energy through the Grimoire—some forces amplify, some counteract, and only the total balance reveals the true potential of her reconstructed spells.

For Example:
Given Power Scores: [2, 1, 3, 4]
Sort Power Scores: [1, 2, 3, 4]
Total Balance: 1 - 2 + 3 - 4 = -2

Part 4 - Binding the Grimoire

The Witch has measured the energies of every combination, oscillated them into harmony, and finally understands the true potential of her spells. Yet the Grimoire still lies in fragments. She knows that not all spell sequences are equal — the order in which she binds her diagrams, tools, ingredients, and candles can magnify the magic to its fullest.

To awaken the Grimoire completely, she must select the most powerful permutation of spells from her previous calculations and arrange them in the optimal ritual order.

Following the ancient rites, she:
1. Chooses one combination of each ingredient, candle, tool, and diagram.
2. Computes the binding score by taking the amount of characters in each type and performing an alternating addition and subtraction.
3. Reorder the ingredients to maximize score and add to a sum.

For example:

Given the combination: ('frog_tongue', 'blood_rune', 'candle_white', 'wand')
11+10-12+4 = 13
We can reorder the lengths to maximize the score:
('frog_tongue', 'blood_rune', 'wand', 'candle_white')
11+10-4+12 = 29

This final score represents the maximum total of stabilized energy of the Grimoire. Only when the Witch performs this ritual correctly will the book of spells awaken fully.