summaryrefslogtreecommitdiff
path: root/Calculator.lua
diff options
context:
space:
mode:
Diffstat (limited to 'Calculator.lua')
-rw-r--r--Calculator.lua32
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,
+}