code.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. type Button struct {
  9. id byte
  10. x, y int
  11. }
  12. type Machine struct {
  13. buttons []Button
  14. x, y int
  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 int) int {
  48. if a < b {
  49. return a
  50. }
  51. return b
  52. }
  53. func calculate(machine Machine, button int) [2]int {
  54. var results [2]int
  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 start > 100 {
  66. start = 100
  67. }
  68. for ; start > 0; 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 otherPushes > 100 {
  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) int {
  86. resultA := calculate(machine, 0)
  87. resultB := calculate(machine, 1)
  88. costA := resultA[0]*3 + resultA[1]
  89. costB := resultB[0]*3 + resultB[1]
  90. return min(costA, costB)
  91. }
  92. func part1(machines []Machine) int {
  93. var result int
  94. for _, machine := range machines {
  95. result += checkMachine(machine)
  96. }
  97. return result
  98. }
  99. func main() {
  100. if len(os.Args) < 2 {
  101. log.Fatal("You need to specify a file!")
  102. }
  103. filePath := os.Args[1]
  104. file, err := os.Open(filePath)
  105. if err != nil {
  106. log.Fatalf("Failed to open %s!\n", filePath)
  107. }
  108. machines := readInput(file)
  109. fmt.Println("Part1:", part1(machines))
  110. }