code.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 checkPlan(plan blueprint) int {
  69. var robots inventory
  70. robots.ore++
  71. var resources inventory
  72. for i := 0; i < 24; i++ {
  73. resources = produce(robots, resources)
  74. if canProduceGeode(plan, resources) {
  75. robots.geode++
  76. resources = produceGeode(plan, resources)
  77. }
  78. if canProduceObsidian(plan, resources) {
  79. robots.obsidian++
  80. resources = produceObsidian(plan, resources)
  81. }
  82. if canProduceClay(plan, resources) {
  83. robots.clay++
  84. resources = produceClay(plan, resources)
  85. }
  86. }
  87. return resources.geode * plan.id
  88. }
  89. func part1(blueprints []blueprint) int {
  90. sum := 0
  91. for i := range blueprints {
  92. sum += checkPlan(blueprints[i])
  93. }
  94. return sum
  95. }
  96. func main() {
  97. if len(os.Args) < 2 {
  98. log.Fatal("You need to specify a file!")
  99. }
  100. filePath := os.Args[1]
  101. file, err := os.Open(filePath)
  102. if err != nil {
  103. log.Fatalf("Failed to open %s!\n", filePath)
  104. }
  105. blueprints := readInput(file)
  106. fmt.Println("Part1:", part1(blueprints))
  107. }