Last active
May 28, 2020 16:35
-
-
Save boppreh/43ae1218b2dfe54a38b83891b968b8ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: