code.go 3.4 KB

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