code.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. type blueprint struct {
  9. id int
  10. oreCost int
  11. clayCost int
  12. obsidianCost [2]int
  13. geodeCost [2]int
  14. }
  15. func readInput(file *os.File) []blueprint {
  16. scanner := bufio.NewScanner(file)
  17. var blueprints []blueprint
  18. for scanner.Scan() {
  19. line := scanner.Text()
  20. if line == "" {
  21. continue
  22. }
  23. var current blueprint
  24. n, err := fmt.Sscanf(line, "Blueprint %d: Each ore robot costs %d ore. Each clay robot costs %d ore. Each obsidian robot costs %d ore and %d clay. Each geode robot costs %d ore and %d obsidian.", &current.id, &current.oreCost, &current.clayCost, &current.obsidianCost[0], &current.obsidianCost[1], &current.geodeCost[0], &current.geodeCost[1])
  25. if n != 7 || err != nil {
  26. log.Fatal("Can't parse:", line, err)
  27. }
  28. blueprints = append(blueprints, current)
  29. }
  30. return blueprints
  31. }
  32. type inventory struct {
  33. ore int
  34. clay int
  35. obsidian int
  36. geode int
  37. }
  38. func canProduceOre(plan blueprint, resources inventory) bool {
  39. return resources.ore >= plan.oreCost
  40. }
  41. func produceOre(plan blueprint, resources inventory) inventory {
  42. resources.ore -= plan.oreCost
  43. return resources
  44. }
  45. func canProduceClay(plan blueprint, resources inventory) bool {
  46. return resources.ore >= plan.clayCost
  47. }
  48. func produceClay(plan blueprint, resources inventory) inventory {
  49. resources.ore -= plan.clayCost
  50. return resources
  51. }
  52. func canProduceObsidian(plan blueprint, resources inventory) bool {
  53. return resources.ore >= plan.obsidianCost[0] && resources.clay >= plan.obsidianCost[1]
  54. }
  55. func produceObsidian(plan blueprint, resources inventory) inventory {
  56. resources.ore -= plan.obsidianCost[0]
  57. resources.clay -= plan.obsidianCost[1]
  58. return resources
  59. }
  60. func canProduceGeode(plan blueprint, resources inventory) bool {
  61. return resources.ore >= plan.geodeCost[0] && resources.obsidian >= plan.geodeCost[1]
  62. }
  63. func produceGeode(plan blueprint, resources inventory) inventory {
  64. resources.ore -= plan.geodeCost[0]
  65. resources.obsidian -= plan.geodeCost[1]
  66. return resources
  67. }
  68. func produce(robots inventory, resources inventory) inventory {
  69. resources.ore += robots.ore
  70. resources.clay += robots.clay
  71. resources.obsidian += robots.obsidian
  72. resources.geode += robots.geode
  73. return resources
  74. }
  75. func shouldProduceOre(plan blueprint, robots inventory, resources inventory) bool {
  76. countWithout := 0
  77. without := resources
  78. for {
  79. if canProduceClay(plan, without) {
  80. break
  81. }
  82. without = produce(robots, without)
  83. countWithout++
  84. }
  85. countWith := 0
  86. with := resources
  87. with = produceOre(plan, with)
  88. robots.ore++
  89. for {
  90. if canProduceClay(plan, with) {
  91. break
  92. }
  93. with = produce(robots, with)
  94. countWith++
  95. }
  96. return countWith <= countWithout
  97. }
  98. func shouldProduceClay(plan blueprint, robots inventory, resources inventory) bool {
  99. if robots.clay == 0 {
  100. return true
  101. }
  102. countWithout := 0
  103. without := resources
  104. for {
  105. if canProduceObsidian(plan, without) {
  106. break
  107. }
  108. without = produce(robots, without)
  109. countWithout++
  110. }
  111. countWith := 0
  112. with := resources
  113. with = produceClay(plan, with)
  114. robots.clay++
  115. for {
  116. if canProduceObsidian(plan, with) {
  117. break
  118. }
  119. with = produce(robots, with)
  120. countWith++
  121. }
  122. return countWith <= countWithout
  123. }
  124. func shouldProduceObsidian(plan blueprint, robots inventory, resources inventory) bool {
  125. if robots.obsidian == 0 {
  126. return true
  127. }
  128. countWithout := 0
  129. without := resources
  130. for {
  131. if canProduceGeode(plan, without) {
  132. break
  133. }
  134. without = produce(robots, without)
  135. countWithout++
  136. }
  137. countWith := 0
  138. with := resources
  139. with = produceObsidian(plan, with)
  140. robots.obsidian++
  141. for {
  142. if canProduceGeode(plan, with) {
  143. break
  144. }
  145. with = produce(robots, with)
  146. countWith++
  147. }
  148. return countWith <= countWithout
  149. }
  150. func checkPlan(plan blueprint) int {
  151. var robots inventory
  152. robots.ore++
  153. var resources inventory
  154. for i := 0; i < 24; i++ {
  155. newRobots := robots
  156. if canProduceGeode(plan, resources) {
  157. newRobots.geode++
  158. resources = produceGeode(plan, resources)
  159. } else if canProduceObsidian(plan, resources) {
  160. if shouldProduceObsidian(plan, robots, resources) {
  161. newRobots.obsidian++
  162. resources = produceObsidian(plan, resources)
  163. }
  164. } else if robots.clay < plan.obsidianCost[1] && canProduceClay(plan, resources) {
  165. if shouldProduceClay(plan, robots, resources) {
  166. newRobots.clay++
  167. resources = produceClay(plan, resources)
  168. }
  169. } else if robots.ore < plan.clayCost && canProduceOre(plan, resources) {
  170. if shouldProduceClay(plan, robots, resources) {
  171. newRobots.ore++
  172. resources = produceOre(plan, resources)
  173. }
  174. }
  175. resources = produce(robots, resources)
  176. fmt.Println(plan.id, i+1, robots, resources)
  177. robots = newRobots
  178. }
  179. fmt.Println(plan.id, resources.geode)
  180. return resources.geode * plan.id
  181. }
  182. func part1(blueprints []blueprint) int {
  183. sum := 0
  184. for i := range blueprints {
  185. sum += checkPlan(blueprints[i])
  186. }
  187. return sum
  188. }
  189. func main() {
  190. if len(os.Args) < 2 {
  191. log.Fatal("You need to specify a file!")
  192. }
  193. filePath := os.Args[1]
  194. file, err := os.Open(filePath)
  195. if err != nil {
  196. log.Fatalf("Failed to open %s!\n", filePath)
  197. }
  198. blueprints := readInput(file)
  199. fmt.Println("Part1:", part1(blueprints))
  200. }