diff options
author | Aki <please@ignore.pl> | 2021-12-22 23:25:36 +0100 |
---|---|---|
committer | Aki <please@ignore.pl> | 2021-12-22 23:25:36 +0100 |
commit | e617261dd12e3db9e2c547040147390a0135d6c5 (patch) | |
tree | cab67a2ccc98dc7e63328fc2794f1d091b4315df /Calculator.lua | |
download | pi-calc-e617261dd12e3db9e2c547040147390a0135d6c5.zip pi-calc-e617261dd12e3db9e2c547040147390a0135d6c5.tar.gz pi-calc-e617261dd12e3db9e2c547040147390a0135d6c5.tar.bz2 |
Diffstat (limited to 'Calculator.lua')
-rw-r--r-- | Calculator.lua | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/Calculator.lua b/Calculator.lua new file mode 100644 index 0000000..aece972 --- /dev/null +++ b/Calculator.lua @@ -0,0 +1,32 @@ +local Recipe = require "Recipe" + + +local +function raw_materials_needed (quantity) + local recipe = Recipe[quantity.material] + if not recipe then + return {[quantity.material]=quantity.amount} + end + local total = {} + for _, input in pairs(recipe.input) do + local subtotal = raw_materials_needed(input) + for material, amount in pairs(subtotal) do + total[material] = (total[material] or 0) + amount * quantity.amount + end + end + return total +end + + +local +function pretty_print (acc) + for material, amount in pairs(acc) do + print(("%d\t%q"):format(amount, material)) + end +end + + +return { + raw_materials_needed=raw_materials_needed, + pretty_print=pretty_print, +} |