code.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. catchUp := resources
  70. for i := 0; i < plan.clayCost; i++ {
  71. catchUp = produce(robots, catchUp)
  72. }
  73. if canProduceObsidian(plan, catchUp) {
  74. return false
  75. }
  76. return true
  77. }
  78. func shouldProduceObsidian(plan blueprint, robots inventory, resources inventory) bool {
  79. catchUp := resources
  80. for i := 0; i < plan.obsidianCost[0]; i++ {
  81. catchUp = produce(robots, catchUp)
  82. }
  83. if canProduceGeode(plan, catchUp) {
  84. return false
  85. }
  86. return true
  87. }
  88. func checkPlan(plan blueprint) int {
  89. var robots inventory
  90. robots.ore++
  91. var resources inventory
  92. for i := 0; i < 24; i++ {
  93. newRobots := robots
  94. if canProduceGeode(plan, resources) {
  95. newRobots.geode++
  96. resources = produceGeode(plan, resources)
  97. }
  98. if canProduceObsidian(plan, resources) {
  99. if shouldProduceObsidian(plan, robots, resources) {
  100. newRobots.obsidian++
  101. resources = produceObsidian(plan, resources)
  102. }
  103. }
  104. if canProduceClay(plan, resources) {
  105. if shouldProduceClay(plan, robots, resources) {
  106. newRobots.clay++
  107. resources = produceClay(plan, resources)
  108. }
  109. }
  110. resources = produce(robots, resources)
  111. fmt.Println(plan.id, i+1, robots, resources)
  112. robots = newRobots
  113. }
  114. fmt.Println(plan.id, resources.geode)
  115. return resources.geode * plan.id
  116. }
  117. func part1(blueprints []blueprint) int {
  118. sum := 0
  119. for i := range blueprints {
  120. sum += checkPlan(blueprints[i])
  121. }
  122. return sum
  123. }
  124. func main() {
  125. if len(os.Args) < 2 {
  126. log.Fatal("You need to specify a file!")
  127. }
  128. filePath := os.Args[1]
  129. file, err := os.Open(filePath)
  130. if err != nil {
  131. log.Fatalf("Failed to open %s!\n", filePath)
  132. }
  133. blueprints := readInput(file)
  134. fmt.Println("Part1:", part1(blueprints))
  135. }