Given a set of partially filled isobutane cans of various sizes, the solver decides which cans to keep and how to redistribute fuel among them so the total empty weight you carry is minimized while all of your fuel is accommodated.
The solver uses a greedy search with backtracking that explores the solution space systematically in three stages.
Cans are first grouped by specification (110 g, 227 g, 450 g) and sorted by fuel level within each group. The algorithm then iterates through every valid combination of how many cans of each size to keep, pruning branches where:
For each candidate set of cans to keep, the solver solves a secondary problem: move fuel from donors (discarded cans, or kept cans that are over capacity) to recipients (kept cans with spare capacity) while minimizing, in order:
This uses a greedy heuristic for an initial feasible solution, then a depth-first search with memoization under a progressively increasing edge budget, so plans with the fewest transfers are found first.
Solutions are compared with a three-part score:
type Score = [emptyWeight, transferCount, totalTransferred]
Comparison is lexicographic: prefer lower empty weight, then fewer transfers, then less total fuel moved.
Overall O(n³ × D × R × E) in the worst case, but early pruning eliminates most branches and a workload estimator caps total work.
A workload estimate acts as a complexity guard:
workload = (lenA + 1) × (lenB + 1) × n
if (workload > 5_000_000) throw Error
This allows roughly 300 cans of mixed sizes, more when one size dominates, and gives sub-second results for typical inputs of 10 to 50 cans.
About 300 cans for mixed scenarios, enforced by the workload limit. Performance degrades noticeably beyond about 200 cans.
The choice of which cans to keep is globally optimal. The transfer plan within that choice is locally optimal and may not explore every possible transfer ordering.
The allocation subproblem is related to bin packing, the transportation problem, and capacity-constrained bipartite assignment. The solver trades guaranteed optimality of the transfer schedule for practical performance, giving excellent results for real-world backpacking scenarios in well under a second.