summaryrefslogtreecommitdiff
path: root/Calculator.lua
blob: aece97211f2504550ba4521109426d9d2581853c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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,
}