Skip to content

Instantly share code, notes, and snippets.

@boppreh
Last active May 28, 2020 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boppreh/43ae1218b2dfe54a38b83891b968b8ce to your computer and use it in GitHub Desktop.
Save boppreh/43ae1218b2dfe54a38b83891b968b8ce to your computer and use it in GitHub Desktop.
def make(goal, book, pantry, costs):
def guess(steps, required_ingredients):
if all(pantry.count(ingredient) >= required_ingredients.count(ingredient) for ingredient in required_ingredients):
yield tuple(sorted(required_ingredients)), steps
for ingredient in set(required_ingredients) & set(book):
new_required_ingredients = required_ingredients + book[ingredient]
new_required_ingredients.remove(ingredient)
yield from guess([ingredient] + steps, new_required_ingredients)
yield from guess([goal], book[goal])
book = {
#'polyjuice': ['fluxweed', 'seeds'],
'liquid_luck': ['tincture', 'tincture', 'powder'],
'laurel': ['seeds', 'seeds'],
'powder': ['laurel'],
'tincture': ['thyme', 'oil'],
}
pantry = ['seeds', 'seeds', 'thyme', 'oil', 'tincture', 'oil', 'thyme']
costs = {'seeds': 3, 'thyme': 10, 'oil': 2, 'tincture': 25}
goal = 'liquid_luck'
print(f'To make {goal}...')
for i, solution in enumerate(dict(make(goal, book, pantry, costs)).items()):
required_ingredients, steps = solution
cost = sum(costs[ingredient] for ingredient in required_ingredients)
print(f'Option {i+1}) Take from the pantry {", ".join(required_ingredients)}, for a total cost of {cost}, then make {", then ".join(steps)}.')
@boppreh
Copy link
Author

boppreh commented May 28, 2020

Output:

To make liquid_luck...
Option 1) Take from the pantry oil, seeds, seeds, thyme, tincture, for a total cost of 43, then make laurel, then powder, then tincture, then liquid_luck.
Option 2) Take from the pantry oil, oil, seeds, seeds, thyme, thyme, for a total cost of 30, then make laurel, then powder, then tincture, then tincture, then liquid_luck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment