day 15
science for hungry people
approach
each ingredient is an object holding its five properties. nums generates, by backtracking, every way to split 100 teaspoons among the ingredients, and _try scales the ingredients by one such split, clamps negative totals to zero and multiplies the four scoring properties.
part 1 is the best score over all splits. part 2 is the same maximum, filtered to the splits whose calories add up to exactly 500.
the trick
enumerate every composition of 100 into as many parts as there are ingredients; the last part is whatever is left.
python
def nums(n, path, res):
if len(path) == len(ingredients) - 1:
res.append(path + [n])
return
for i in range(1, n):
path.append(i)
nums(n - i, path, res)
path.pop()