code.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 canProduceClay(plan blueprint, resources inventory) bool {
  39. return resources.ore >= plan.clayCost
  40. }
  41. func produceClay(plan blueprint, resources inventory) inventory {
  42. resources.ore -= plan.clayCost
  43. return resources
  44. }
  45. func canProduceObsidian(plan blueprint, resources inventory) bool {
  46. return resources.ore >= plan.obsidianCost[0] && resources.clay >= plan.obsidianCost[1]
  47. }
  48. func produceObsidian(plan blueprint, resources inventory) inventory {
  49. resources.ore -= plan.obsidianCost[0]
  50. resources.clay -= plan.obsidianCost[1]
  51. return resources
  52. }
  53. func canProduceGeode(plan blueprint, resources inventory) bool {
  54. return resources.ore >= plan.geodeCost[0] && resources.obsidian >= plan.geodeCost[1]
  55. }
  56. func produceGeode(plan blueprint, resources inventory) inventory {
  57. resources.ore -= plan.geodeCost[0]
  58. resources.obsidian -= plan.geodeCost[1]
  59. return resources
  60. }
  61. func produce(robots inventory, resources inventory) inventory {
  62. resources.ore += robots.ore
  63. resources.clay += robots.clay
  64. resources.obsidian += robots.obsidian
  65. resources.geode += robots.geode
  66. return resources
  67. }
  68. func shouldProduceClay(plan blueprint, robots inventory, resources inventory) bool {
  69. if robots.clay == 0 {
  70. return true
  71. }
  72. countWithout := 0
  73. without := resources
  74. for {
  75. if canProduceObsidian(plan, without) {
  76. break
  77. }
  78. without = produce(robots, without)
  79. countWithout++
  80. }
  81. countWith := 0
  82. with := resources
  83. with = produceClay(plan, with)
  84. robots.clay++
  85. for {
  86. if canProduceObsidian(plan, with) {
  87. break
  88. }
  89. with = produce(robots, with)
  90. countWith++
  91. }
  92. return countWith < countWithout
  93. }
  94. func shouldProduceObsidian(plan blueprint, robots inventory, resources inventory) bool {
  95. if robots.obsidian == 0 {
  96. return true
  97. }
  98. countWithout := 0
  99. without := resources
  100. for {
  101. if canProduceGeode(plan, without) {
  102. break
  103. }
  104. without = produce(robots, without)
  105. countWithout++
  106. }
  107. countWith := 0
  108. with := resources
  109. with = produceObsidian(plan, with)
  110. robots.obsidian++
  111. for {
  112. if canProduceGeode(plan, with) {
  113. break
  114. }
  115. with = produce(robots, with)
  116. countWith++
  117. }
  118. return countWith < countWithout
  119. }
  120. func checkPlan(plan blueprint) int {
  121. var robots inventory
  122. robots.ore++
  123. var resources inventory
  124. for i := 0; i < 24; i++ {
  125. newRobots := robots
  126. if canProduceGeode(plan, resources) {
  127. newRobots.geode++
  128. resources = produceGeode(plan, resources)
  129. }
  130. if canProduceObsidian(plan, resources) {
  131. if shouldProduceObsidian(plan, robots, resources) {
  132. newRobots.obsidian++
  133. resources = produceObsidian(plan, resources)
  134. }
  135. }
  136. if canProduceClay(plan, resources) {
  137. if shouldProduceClay(plan, robots, resources) {
  138. newRobots.clay++
  139. resources = produceClay(plan, resources)
  140. }
  141. }
  142. resources = produce(robots, resources)
  143. fmt.Println(plan.id, i+1, robots, resources)
  144. robots = newRobots
  145. }
  146. fmt.Println(plan.id, resources.geode)
  147. return resources.geode * plan.id
  148. }
  149. func part1(blueprints []blueprint) int {
  150. sum := 0
  151. for i := range blueprints {
  152. sum += checkPlan(blueprints[i])
  153. }
  154. return sum
  155. }
  156. func main() {
  157. if len(os.Args) < 2 {
  158. log.Fatal("You need to specify a file!")
  159. }
  160. filePath := os.Args[1]
  161. file, err := os.Open(filePath)
  162. if err != nil {
  163. log.Fatalf("Failed to open %s!\n", filePath)
  164. }
  165. blueprints := readInput(file)
  166. fmt.Println("Part1:", part1(blueprints))
  167. }