Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. import math
  2. from pprint import *
  3. filename = "input.txt"
  4. file = open(filename)
  5. data = file.read()[:-1]
  6. class Ingredient:
  7. def __init__(self, material, amount):
  8. self.material = material
  9. self.amount = amount
  10.  
  11. def __str__(self):
  12. return self.material + ": " + str(self.amount)
  13.  
  14. def __repr__(self):
  15. return str(self)
  16.  
  17. class Recipe:
  18. def __init__(self,array):
  19. ins = array[0]
  20. outs = array[1][0]
  21. self.materials = []
  22. for i in ins:
  23. self.materials.append(Ingredient(i[1],int(i[0])))
  24. self.output = Ingredient(outs[1],int(outs[0]))
  25.  
  26. def __str__(self):
  27. string = str(self.output)
  28. for material in self.materials:
  29. string = string + "\n " + str(material)
  30. return string
  31.  
  32. def __repr__(self):
  33. return str(self)
  34.  
  35. data = data.split("\n")
  36. for i in range(len(data)):
  37. datam = data[i]
  38. datam = datam.split(" => ")
  39.  
  40. for o in range(len(datam)):
  41. part = datam[o]
  42. part = part.split(", ")
  43. for p in range(len(part)):
  44. part[p] = part[p].split(" ")
  45. datam[o] = part
  46. data[i] = datam
  47.  
  48. recipes = []
  49. index = 0
  50. for datam in data:
  51. recipes.append(Recipe(datam))
  52. if recipes[-1].output.material == "FUEL":
  53. index = len(recipes)-1
  54.  
  55. fuelRecipe = recipes[index]
  56.  
  57. stuffGot = []
  58.  
  59. for recipe in recipes:
  60. stuffGot.append(Ingredient(recipe.output.material,0))
  61. print(recipe)
  62. print()
  63.  
  64.  
  65. def IndexOf(stuff):
  66. for got in stuffGot:
  67. if got.material == stuff:
  68. return stuffGot.index(got)
  69. return -1
  70.  
  71. def AmountOf(stuff):
  72. index = IndexOf(stuff)
  73. if index == -1:
  74. return 0
  75. return stuffGot[index].amount
  76.  
  77. def GetRecipe(stuff):
  78. for recipe in recipes:
  79. if recipe.output.material == stuff:
  80. return recipe
  81. return None
  82.  
  83. usedOre = 0
  84. def MakeMore(stuff):
  85. global usedOre
  86. global stuffGot
  87.  
  88. recipe = GetRecipe(stuff)
  89. for mat in recipe.materials:
  90. if mat.material == "ORE":
  91. usedOre = usedOre + mat.amount
  92. continue
  93. while AmountOf(mat.material) < mat.amount:
  94. MakeMore(mat.material)
  95.  
  96. index = IndexOf(mat.material)
  97. stuffGot[index].amount = stuffGot[index].amount - mat.amount
  98. mainIndex = IndexOf(recipe.output.material)
  99. stuffGot[mainIndex].amount = stuffGot[mainIndex].amount + recipe.output.amount
  100.  
  101. fuelsNeeded = 1000
  102. for i in range(fuelsNeeded):
  103. MakeMore("FUEL")
  104. print("Ore used: " + str(usedOre))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement