code.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. }
  38. }
  39. machines = append(machines, machine)
  40. return machines
  41. }
  42. func main() {
  43. if len(os.Args) < 2 {
  44. log.Fatal("You need to specify a file!")
  45. }
  46. filePath := os.Args[1]
  47. file, err := os.Open(filePath)
  48. if err != nil {
  49. log.Fatalf("Failed to open %s!\n", filePath)
  50. }
  51. machines := readInput(file)
  52. fmt.Println(machines)
  53. }