Skip to content

Instantly share code, notes, and snippets.

@zealws
Created October 23, 2015 04:27
Show Gist options
  • Save zealws/0fe5b7869b0c52c5cd02 to your computer and use it in GitHub Desktop.
Save zealws/0fe5b7869b0c52c5cd02 to your computer and use it in GitHub Desktop.
import math, copy, time as _time
ingredients = "ingredients"
time = "time"
produces = "produces"
rate = "rate"
total = "total"
feeds = "feeds"
leaf = "leaf"
bldgs = "buildings"
recipes = {
"science-pack-1": {
ingredients: {
"copper-plate": 1,
"iron-gear-wheel": 1,
},
time: 5,
},
"science-pack-2": {
ingredients: {
"inserter": 1,
"transport-belt": 1,
},
time: 6,
},
"science-pack-3": {
ingredients: {
"battery": 1,
"advanced-circuit": 1,
"smart-inserter": 1,
"steel-plate": 1,
},
time: 12,
},
"science-pack-4": {
ingredients: {
"alien-artifact": 1,
},
time: 12,
},
"iron-gear-wheel": {
ingredients: {
"iron-plate": 2,
},
},
"inserter": {
ingredients: {
"electronic-circuit": 1,
"iron-gear-wheel": 1,
"iron-plate": 1,
},
},
"electronic-circuit": {
ingredients: {
"iron-plate": 1,
"copper-cable": 3,
},
},
"battery": {
ingredients: {
"copper-plate": 1,
"iron-plate": 1,
"sulfuric-acid": 2,
},
time: 5,
},
"smart-inserter": {
ingredients: {
"fast-inserter": 1,
"electronic-circuit": 4,
},
},
"fast-inserter": {
ingredients: {
"electronic-circuit": 2,
"iron-plate": 2,
"inserter": 1,
},
},
"copper-cable": {
ingredients: {
"copper-plate": 1,
},
produces: 2,
},
"advanced-circuit": {
ingredients: {
"electronic-circuit": 2,
"plastic-bar": 2,
"copper-cable": 4,
},
time: 8,
},
"transport-belt": {
ingredients: {
"iron-plate": 1,
"iron-gear-wheel": 1,
},
produces: 2,
},
}
TIME_UNIT = 1
for item in recipes.keys():
if time not in recipes[item]:
recipes[item][time] = 0.5
if produces not in recipes[item]:
recipes[item][produces] = 1
recipes[item][rate] = float(recipes[item][produces]) / recipes[item][time] * TIME_UNIT
needs = {
"science-pack-1": 1,
"science-pack-2": 1,
"science-pack-3": 1,
# "science-pack-4": 1,
}
results = {}
def calc_requirements(need, count):
if need not in recipes:
if need not in results:
results[need] = {
feeds: [],
total: count,
leaf: True,
}
else:
results[need][total] += count
return
recipe = recipes[need]
if need not in results:
results[need] = copy.deepcopy(recipe)
results[need][feeds] = []
results[need][total] = count
else:
results[need][total] += count
results[need][bldgs] = math.ceil(results[need][total] / recipe[rate])
for i, c in recipe[ingredients].iteritems():
calc_requirements(i,c*count)
if need not in results[i][feeds]:
results[i][feeds] += [need]
for need, count in needs.iteritems():
calc_requirements(need, count)
import json
print json.dumps(results, sort_keys=True, indent=2)
{
"advanced-circuit": {
"buildings": 8.0,
"feeds": [
"science-pack-3"
],
"ingredients": {
"copper-cable": 4,
"electronic-circuit": 2,
"plastic-bar": 2
},
"produces": 1,
"rate": 0.125,
"time": 8,
"total": 1
},
"battery": {
"buildings": 5.0,
"feeds": [
"science-pack-3"
],
"ingredients": {
"copper-plate": 1,
"iron-plate": 1,
"sulfuric-acid": 2
},
"produces": 1,
"rate": 0.2,
"time": 5,
"total": 1
},
"copper-cable": {
"buildings": 9.0,
"feeds": [
"electronic-circuit",
"advanced-circuit"
],
"ingredients": {
"copper-plate": 1
},
"produces": 2,
"rate": 4.0,
"time": 0.5,
"total": 34
},
"copper-plate": {
"feeds": [
"science-pack-1",
"battery",
"copper-cable"
],
"leaf": true,
"total": 36
},
"electronic-circuit": {
"buildings": 5.0,
"feeds": [
"inserter",
"fast-inserter",
"smart-inserter",
"advanced-circuit"
],
"ingredients": {
"copper-cable": 3,
"iron-plate": 1
},
"produces": 1,
"rate": 2.0,
"time": 0.5,
"total": 10
},
"fast-inserter": {
"buildings": 1.0,
"feeds": [
"smart-inserter"
],
"ingredients": {
"electronic-circuit": 2,
"inserter": 1,
"iron-plate": 2
},
"produces": 1,
"rate": 2.0,
"time": 0.5,
"total": 1
},
"inserter": {
"buildings": 1.0,
"feeds": [
"fast-inserter",
"science-pack-2"
],
"ingredients": {
"electronic-circuit": 1,
"iron-gear-wheel": 1,
"iron-plate": 1
},
"produces": 1,
"rate": 2.0,
"time": 0.5,
"total": 2
},
"iron-gear-wheel": {
"buildings": 2.0,
"feeds": [
"science-pack-1",
"inserter",
"transport-belt"
],
"ingredients": {
"iron-plate": 2
},
"produces": 1,
"rate": 2.0,
"time": 0.5,
"total": 4
},
"iron-plate": {
"feeds": [
"iron-gear-wheel",
"battery",
"electronic-circuit",
"inserter",
"fast-inserter",
"transport-belt"
],
"leaf": true,
"total": 24
},
"plastic-bar": {
"feeds": [
"advanced-circuit"
],
"leaf": true,
"total": 2
},
"science-pack-1": {
"buildings": 5.0,
"feeds": [],
"ingredients": {
"copper-plate": 1,
"iron-gear-wheel": 1
},
"produces": 1,
"rate": 0.2,
"time": 5,
"total": 1
},
"science-pack-2": {
"buildings": 6.0,
"feeds": [],
"ingredients": {
"inserter": 1,
"transport-belt": 1
},
"produces": 1,
"rate": 0.16666666666666666,
"time": 6,
"total": 1
},
"science-pack-3": {
"buildings": 12.0,
"feeds": [],
"ingredients": {
"advanced-circuit": 1,
"battery": 1,
"smart-inserter": 1,
"steel-plate": 1
},
"produces": 1,
"rate": 0.08333333333333333,
"time": 12,
"total": 1
},
"smart-inserter": {
"buildings": 1.0,
"feeds": [
"science-pack-3"
],
"ingredients": {
"electronic-circuit": 4,
"fast-inserter": 1
},
"produces": 1,
"rate": 2.0,
"time": 0.5,
"total": 1
},
"steel-plate": {
"feeds": [
"science-pack-3"
],
"leaf": true,
"total": 1
},
"sulfuric-acid": {
"feeds": [
"battery"
],
"leaf": true,
"total": 2
},
"transport-belt": {
"buildings": 1.0,
"feeds": [
"science-pack-2"
],
"ingredients": {
"iron-gear-wheel": 1,
"iron-plate": 1
},
"produces": 2,
"rate": 4.0,
"time": 0.5,
"total": 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment