code.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. type Button struct {
  9. id byte
  10. x, y int64
  11. }
  12. type Machine struct {
  13. buttons []Button
  14. x, y int64
  15. }
  16. func readInput(file *os.File) []Machine {
  17. scanner := bufio.NewScanner(file)
  18. var machines []Machine
  19. var machine Machine
  20. var buttonsRead int
  21. for scanner.Scan() {
  22. line := scanner.Text()
  23. if line == "" {
  24. buttonsRead = 0
  25. machines = append(machines, machine)
  26. machine = Machine{}
  27. continue
  28. }
  29. if buttonsRead < 2 {
  30. var button Button
  31. n, err := fmt.Sscanf(line, "Button %c: X+%d, Y+%d", &button.id, &button.x, &button.y)
  32. if n != 3 || err != nil {
  33. log.Fatalf("Not able to parse button '%s': %s", line, err)
  34. }
  35. machine.buttons = append(machine.buttons, button)
  36. buttonsRead++
  37. } else {
  38. n, err := fmt.Sscanf(line, "Prize: X=%d, Y=%d", &machine.x, &machine.y)
  39. if n != 2 || err != nil {
  40. log.Fatalf("Not able to parse machine '%s': %s", line, err)
  41. }
  42. }
  43. }
  44. machines = append(machines, machine)
  45. return machines
  46. }
  47. func min(a, b int64) int64 {
  48. if a < b {
  49. return a
  50. }
  51. return b
  52. }
  53. func calculate(machine Machine, button int, limit int64, minimum int64) [2]int64 {
  54. var results [2]int64
  55. otherButton := (button + 1) % 2
  56. if machine.x%machine.buttons[button].x == 0 && machine.y%machine.buttons[button].y == 0 {
  57. pushes := machine.x / machine.buttons[button].x
  58. if pushes*machine.buttons[button].y == machine.y {
  59. results[button] = pushes
  60. results[otherButton] = 0
  61. return results
  62. }
  63. }
  64. start := min(machine.x/machine.buttons[button].x, machine.y/machine.buttons[button].y)
  65. if limit > 0 && start > limit {
  66. start = limit
  67. }
  68. for ; start > minimum; start-- {
  69. deltaX := machine.x - start*machine.buttons[button].x
  70. if deltaX%machine.buttons[otherButton].x == 0 {
  71. otherPushes := deltaX / machine.buttons[otherButton].x
  72. if limit > 0 && otherPushes > limit {
  73. continue
  74. }
  75. if machine.y-start*machine.buttons[button].y != otherPushes*machine.buttons[otherButton].y {
  76. continue
  77. }
  78. results[button] = start
  79. results[otherButton] = otherPushes
  80. return results
  81. }
  82. }
  83. return results
  84. }
  85. func checkMachine(machine Machine, limit int64, modifier int64, minimum int64) int64 {
  86. machine.x += modifier
  87. machine.y += modifier
  88. resultA := calculate(machine, 0, limit, minimum)
  89. resultB := calculate(machine, 1, limit, minimum)
  90. costA := resultA[0]*3 + resultA[1]
  91. costB := resultB[0]*3 + resultB[1]
  92. return min(costA, costB)
  93. }
  94. func solve(machines []Machine, limit int64, modifier int64, minimum int64) int64 {
  95. var result int64
  96. for _, machine := range machines {
  97. result += checkMachine(machine, limit, modifier, minimum)
  98. }
  99. return result
  100. }
  101. func main() {
  102. if len(os.Args) < 2 {
  103. log.Fatal("You need to specify a file!")
  104. }
  105. filePath := os.Args[1]
  106. file, err := os.Open(filePath)
  107. if err != nil {
  108. log.Fatalf("Failed to open %s!\n", filePath)
  109. }
  110. machines := readInput(file)
  111. fmt.Println("Part1:", solve(machines, 100, 0, 0))
  112. fmt.Println("Part2:", solve(machines, -1, 10000000000000, 100000000000))
  113. }