code.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. results[button] = machine.x / machine.buttons[button].x
  58. results[otherButton] = 0
  59. return results
  60. }
  61. start := min(machine.x/machine.buttons[button].x, machine.y/machine.buttons[button].y)
  62. if start > 100 {
  63. start = 100
  64. }
  65. for start > 0 {
  66. if (machine.x-start*machine.buttons[button].x)%machine.buttons[otherButton].x == 0 && (machine.y-start*machine.buttons[button].y)%machine.buttons[otherButton].y == 0 {
  67. otherPushes := (machine.x - start*machine.buttons[button].x) / machine.buttons[otherButton].x
  68. if otherPushes <= 100 {
  69. results[button] = start
  70. results[otherButton] = otherPushes
  71. return results
  72. }
  73. }
  74. start--
  75. }
  76. return results
  77. }
  78. func checkMachine(machine Machine) int {
  79. resultA := calculate(machine, 0)
  80. resultB := calculate(machine, 1)
  81. costA := resultA[0]*3 + resultA[1]
  82. costB := resultB[0]*3 + resultB[1]
  83. return min(costA, costB)
  84. }
  85. func part1(machines []Machine) int {
  86. var result int
  87. for _, machine := range machines {
  88. result += checkMachine(machine)
  89. }
  90. return result
  91. }
  92. func main() {
  93. if len(os.Args) < 2 {
  94. log.Fatal("You need to specify a file!")
  95. }
  96. filePath := os.Args[1]
  97. file, err := os.Open(filePath)
  98. if err != nil {
  99. log.Fatalf("Failed to open %s!\n", filePath)
  100. }
  101. machines := readInput(file)
  102. fmt.Println("Part1:", part1(machines))
  103. }