1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- package main
- import (
- "bufio"
- "fmt"
- "log"
- "os"
- )
- type Button struct {
- id byte
- x, y int
- }
- type Machine struct {
- buttons []Button
- x, y int
- }
- func readInput(file *os.File) []Machine {
- scanner := bufio.NewScanner(file)
- var machines []Machine
- var machine Machine
- var buttonsRead int
- for scanner.Scan() {
- line := scanner.Text()
- if line == "" {
- buttonsRead = 0
- machines = append(machines, machine)
- machine = Machine{}
- continue
- }
- if buttonsRead < 2 {
- var button Button
- n, err := fmt.Sscanf(line, "Button %c: X+%d, Y+%d", &button.id, &button.x, &button.y)
- if n != 3 || err != nil {
- log.Fatalf("Not able to parse button '%s': %s", line, err)
- }
- machine.buttons = append(machine.buttons, button)
- buttonsRead++
- }
- }
- machines = append(machines, machine)
- return machines
- }
- func main() {
- if len(os.Args) < 2 {
- log.Fatal("You need to specify a file!")
- }
- filePath := os.Args[1]
- file, err := os.Open(filePath)
- if err != nil {
- log.Fatalf("Failed to open %s!\n", filePath)
- }
- machines := readInput(file)
- fmt.Println(machines)
- }
|