#!/usr/bin/env ruby

require 'json'
Products = JSON.parse(DATA.read)
Productindex = Products.keys

ingredientsstack = []
buildstack = {}

build = ARGV.shift
if build.nil? then
  puts "Build what?"
  exit 1
end

quantity = ARGV.shift
if quantity.nil? then
  quantity = 1
else
  quantity = quantity.to_i
end

ingredientsstack.push build

while ingredientsstack.count > 0 do
  build = ingredientsstack.pop
  product = Products[build]
  if product.nil? then
    puts "#{build} is not a product"
    exit 1
  end
  product.each do |key, value|
    value.times do
      ingredientsstack.push key
    end
  end
  if buildstack[build].nil? then
    buildstack[build] = 1
  else
    buildstack[build] += 1
  end
end

buildstack.keys.sort { |a, b|
  Productindex.index(a) <=> Productindex.index(b)
}.each do |key|
  puts "%4d: %s" % [buildstack[key] * quantity, key]
end

__END__
{
  "Cardboard": {},
  "Concrete": {},
  "Magnet": {},
  "Metal": {},
  "Paint": {},
  "Plastic": {},
  "Rare metal": {},
  "Rubber": {},
  "Wire": {},

  "Adv concrete": {
    "Concrete": 2,
    "Metal": 2
  },
  "Belt": {
    "Rubber": 2
  },
  "Box": {
    "Cardboard": 4
  },
  "Cable": {
    "Metal": 1
  },
  "Circuit": {
    "Plastic": 2,
    "Wire": 1
  },
  "Gear": {
    "Metal": 2
  },
  "Hose": {
    "Rubber": 2
  },
  "Metal wheel": {
    "Metal": 2
  },
  "Motor": {
    "Magnet": 2,
    "Metal": 1,
    "Wire": 2
  },
  "Plastic wheel": {
    "Plastic": 2
  },
  "Pump": {
    "Metal": 2,
    "Plastic": 1,
    "Rubber": 1
  },
  "Garden gnome": {
    "Concrete": 1,
    "Paint": 1,
    "Box": 1
  },
  "Speakers": {
    "Magnet": 2,
    "Plastic": 2,
    "Wire": 1,
    "Box": 1
  },
  "Toaster": {
    "Metal": 2,
    "Wire": 2,
    "Box": 1
  },
  "Air gun": {
    "Metal": 4,
    "Hose": 2
  },
  "Arm": {
    "Metal": 2,
    "Circuit": 1,
    "Motor": 2
  },
  "Conveyor": {
    "Belt": 1,
    "Gear": 2,
    "Metal wheel": 8,
    "Motor": 1
  },
  "Lifter": {
    "Metal": 3,
    "Cable": 2,
    "Hose": 2,
    "Motor": 1,
    "Pump": 1
  },
  "Logic unit": {
    "Wire": 5,
    "Circuit": 4
  },
  "Mover": {
    "Metal": 3,
    "Gear": 4,
    "Metal wheel": 4,
    "Motor": 2
  },
  "Road": {
    "Concrete": 4,
    "Adv concrete": 2
  },
  "Support": {
    "Metal": 2,
    "Adv concrete": 2
  },
  "Thing-a-ma-jig": {
    "Circuit": 3,
    "Hose": 2,
    "Motor": 1,
    "Pump": 2
  },
  "Widget": {
    "Metal": 1,
    "Plastic": 4,
    "Wire": 2,
    "Circuit": 1
  },
  "Toy car": {
    "Metal": 1,
    "Paint": 1,
    "Plastic": 3,
    "Plastic wheel": 4,
    "Box": 1
  },
  "Water gun": {
    "Paint": 1,
    "Plastic": 6,
    "Hose": 2,
    "Box": 1
  },

  "Adv logic unit": {
    "Wire": 4,
    "Circuit": 4,
    "Logic unit": 2
  },
  "Assembly line": {
    "Air gun": 2,
    "Arm": 2,
    "Conveyor": 3,
    "Lifter": 1,
    "Mover": 1
  },
  "Jet engine": {
    "Metal": 8,
    "Wire": 12,
    "Hose": 6,
    "Pump": 4,
    "Thing-a-ma-jig": 1
  },
  "Sensor": {
    "Rare metal": 2,
    "Wire": 1,
    "Circuit": 1,
    "Logic unit": 1
  },
  "Bridge": {
    "Road": 6,
    "Support": 6
  },
  "Forklift": {
    "Metal": 6,
    "Rubber": 8,
    "Metal wheel": 4,
    "Motor": 2,
    "Box": 4
  },
  "Radio tower": {
    "Metal": 12,
    "Wire": 6,
    "Support": 4
  },
  "Tablet computer": {
    "Plastic": 1,
    "Wire": 3,
    "Circuit": 3,
    "Logic unit": 1,
    "Box": 1
  },

  "Drone": {
    "Plastic": 4,
    "Rare metal": 1,
    "Motor": 4,
    "Adv logic unit": 1,
    "Sensor": 2
  },
  "Jet": {
    "Metal": 24,
    "Wire": 18,
    "Adv logic unit": 6,
    "Jet engine": 4,
    "Sensor": 8
  },
  "Oculus rift": {
    "Plastic": 2,
    "Rare metal": 2,
    "Wire": 4,
    "Widget": 2,
    "Sensor": 2
  },

  "Builder: Tier 1": {
    "Motor": 1,
    "Arm": 2,
    "Conveyor": 1,
    "Thing-a-ma-jig": 1
  },
  "Builder: Tier 2": {
    "Motor": 1,
    "Arm": 3,
    "Conveyor": 1,
    "Thing-a-ma-jig": 1,
    "Widget": 2
  },
  "Builder: Tier 3": {
    "Logic unit": 1,
    "Thing-a-ma-jig": 1,
    "Widget": 4,
    "Assembly line": 1
  },
  "Builder: Tier 4": {
    "Widget": 2,
    "Adv logic unit": 2,
    "Assembly line": 1,
    "Sensor": 6
  },
  "Builder: Builders": {
    "Arm": 2,
    "Logic unit": 1,
    "Thing-a-ma-jig": 4,
    "Widget": 1,
    "Assembly line": 1
  },
  "Builder: Taskers": {
    "Thing-a-ma-jig": 2,
    "Widget": 2,
    "Adv logic unit": 1,
    "Assembly line": 1
  },
  "Builder: Universal": {
    "Arm": 4,
    "Thing-a-ma-jig": 2,
    "Widget": 4,
    "Adv logic unit": 2,
    "Assembly line": 2
  },

  "Tasker: Purchasing": {
    "Paint": 1,
    "Wire": 6,
    "Circuit": 4,
    "Logic unit": 1,
    "Widget": 1
  },
  "Tasker: Sales": {
    "Paint": 1,
    "Wire": 8,
    "Logic unit": 2,
    "Widget": 1,
    "Adv logic unit": 1
  }
}