Skip to content

Instantly share code, notes, and snippets.

@skairunner
Created May 5, 2019 07:05
Show Gist options
  • Save skairunner/88d8653e059f7038eedf3cfddcf066c7 to your computer and use it in GitHub Desktop.
Save skairunner/88d8653e059f7038eedf3cfddcf066c7 to your computer and use it in GitHub Desktop.
Minecraft calculator
import json
import sys
import math
from fractions import Fraction
def craft(item, number, recipes, L=None):
if L != None and L <= 0:
return {item: number}
if item not in recipes:
return {item: number}
# otherwise, do the recursion
out = {}
recipe = recipes[item]
for ingredient, n in recipe.items():
if L != None:
result = craft(ingredient, n*number, recipes, L-1)
else:
result = craft(ingredient, n*number, recipes)
for item, m in result.items():
if item in out:
out[item] += m
else:
out[item] = m
return out
def printnice(result):
for ingredient, n in result.items():
print("{0:>5} {1}".format(math.ceil(n), ingredient))
def loadRecipes(filename):
with open(filename) as f:
recipes = json.load(f)
for item in recipes:
recipe = recipes[item]
editlist = []
for ingredient, n in recipe.items():
if type(n) == str:
editlist.append((ingredient, n))
for ingredient, n in editlist:
recipe[ingredient] = Fraction(n)
return recipes
if __name__=="__main__":
recipes = loadRecipes("items.json")
itemname = sys.argv[1]
printnice(craft(itemname, 1, recipes))
{
"reinforced hull": {
"reinforced metal": 5,
"reinforced wheels": 2
},
"reinforced wheels": {
"iron ingot": 4,
"reinforced metal": 1
},
"reinforced metal": {
"iron ingot": 1,
"hardened mesh": "1/5",
"refined hardener": "3/5"
},
"refined hardener": {
"obsidian": 2,
"diamond": "1/2"
},
"hardened mesh": {
"iron bars": 5,
"refined hardener": 4
},
"iron bars": {
"iron ingot": "6/16"
},
"conductive iron": {
"iron ingot": 1,
"redstone dust": 1
},
"leather strap": {
"iron ingot": 2,
"leather": 4
},
"basic capacitor": {
"copper ingot": 1,
"redstone": 4,
"gold nugget": 2
},
"conductive iron thruster": {
"conductive iron": 2,
"basic capacitor": 2,
"basic gear": 2,
"insulated redstone conduit": 2
},
"basic gear": {
"cobblestone": 4,
"stick": 4
},
"insulated redstone conduit": {
"redstone alloy": 3,
"conduit binder": 6
},
"redstone alloy": {
"redstone dust": 1,
"silicon": 1
},
"conductive iron jetpack": {
"conductive iron": 4,
"leather strap": 1,
"basic capacitor": 1,
"conductive iron thruster": 2
},
"electrical steel jetpack": {
"electrical steel": 4,
"basic capacitor": 1,
"electrical steel thruster": 2,
"conductive iron jetpack": 1
},
"electrical steel thruster": {
"electrical steel": 2,
"energy conduit": 2,
"machine chassis": 2,
"redstone dust": 1,
"basic capacitor": 2
},
"machine chassis": {
"iron bars": 4,
"iron ingot": 4,
"basic capacitor": 1
},
"electrical steel": {
"iron ingot": 1,
"coal": 1,
"silicon": 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment