code.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 shouldProduce(currentCount int, plan blueprint, robots inventory, robotsWith inventory, resources inventory, resourcesWith inventory, check func(blueprint, inventory) bool) bool {
  76. if currentCount == 0 {
  77. return true
  78. }
  79. countWithout := 0
  80. for {
  81. if check(plan, resources) {
  82. break
  83. }
  84. resources = produce(robots, resources)
  85. countWithout++
  86. }
  87. countWith := 0
  88. for {
  89. if check(plan, resourcesWith) {
  90. break
  91. }
  92. resourcesWith = produce(robotsWith, resourcesWith)
  93. countWith++
  94. }
  95. return countWith <= countWithout
  96. }
  97. func shouldProduceOre(plan blueprint, robots inventory, resources inventory) bool {
  98. robotsWith := robots
  99. robotsWith.ore++
  100. resourcesWith := resources
  101. resourcesWith = produceOre(plan, resourcesWith)
  102. return shouldProduce(robots.ore, plan, robots, robotsWith, resources, resourcesWith, canProduceClay)
  103. }
  104. func shouldProduceClay(plan blueprint, robots inventory, resources inventory) bool {
  105. robotsWith := robots
  106. robotsWith.clay++
  107. resourcesWith := resources
  108. resourcesWith = produceClay(plan, resourcesWith)
  109. return shouldProduce(robots.clay, plan, robots, robotsWith, resources, resourcesWith, canProduceObsidian)
  110. }
  111. func shouldProduceObsidian(plan blueprint, robots inventory, resources inventory) bool {
  112. robotsWith := robots
  113. robotsWith.obsidian++
  114. resourcesWith := resources
  115. resourcesWith = produceObsidian(plan, resourcesWith)
  116. return shouldProduce(robots.obsidian, plan, robots, robotsWith, resources, resourcesWith, canProduceGeode)
  117. }
  118. func checkPlan(plan blueprint) int {
  119. var robots inventory
  120. robots.ore++
  121. var resources inventory
  122. for i := 0; i < 24; i++ {
  123. newRobots := robots
  124. if canProduceGeode(plan, resources) {
  125. newRobots.geode++
  126. resources = produceGeode(plan, resources)
  127. } else if canProduceObsidian(plan, resources) {
  128. if shouldProduceObsidian(plan, robots, resources) {
  129. newRobots.obsidian++
  130. resources = produceObsidian(plan, resources)
  131. }
  132. } else if robots.clay < plan.obsidianCost[1] && canProduceClay(plan, resources) {
  133. if shouldProduceClay(plan, robots, resources) {
  134. newRobots.clay++
  135. resources = produceClay(plan, resources)
  136. }
  137. } else if robots.ore < plan.clayCost && canProduceOre(plan, resources) {
  138. if shouldProduceClay(plan, robots, resources) {
  139. newRobots.ore++
  140. resources = produceOre(plan, resources)
  141. }
  142. }
  143. resources = produce(robots, resources)
  144. fmt.Println(plan.id, i+1, robots, resources)
  145. robots = newRobots
  146. }
  147. fmt.Println(plan.id, resources.geode)
  148. return resources.geode * plan.id
  149. }
  150. func part1(blueprints []blueprint) int {
  151. sum := 0
  152. for i := range blueprints {
  153. sum += checkPlan(blueprints[i])
  154. }
  155. return sum
  156. }
  157. func main() {
  158. if len(os.Args) < 2 {
  159. log.Fatal("You need to specify a file!")
  160. }
  161. filePath := os.Args[1]
  162. file, err := os.Open(filePath)
  163. if err != nil {
  164. log.Fatalf("Failed to open %s!\n", filePath)
  165. }
  166. blueprints := readInput(file)
  167. fmt.Println("Part1:", part1(blueprints))
  168. }