Module day14

Source
Expand description

§Space Stoichiometry

Sorting the reactions in topological order from FUEL at the start to ORE at the end, allows us to process each reaction only once.

Structs§

Ingredient 🔒
Reaction

Functions§

ore 🔒
Run the reactions to find ore needed. Each chemical is processed only once, so we don’t need to track excess values of intermediate chemicals.
parse
To speed things up when processing, we use a temporary map to convert chemical names into contiguous indices.
part1
Calculate the amount of ore needed for 1 fuel. This will be the most ore needed per unit of fuel. Larger amounts of fuel can use some of the leftover chemicals from intermediate reactions.
part2
Find the maximum amount of fuel possible from 1 trillion ore with an efficient binary search.
topological 🔒
Sort reactions in topological order from FUEL at the root to ORE at the leaves. Reactions may occur more than once at different depths in the graph, so we take the maximum depth.