Browse Source

Able to read buttons

Piotr Czajkowski 3 weeks ago
parent
commit
9abc0d4e12
1 changed files with 65 additions and 0 deletions
  1. 65 0
      13/code.go

+ 65 - 0
13/code.go

@@ -0,0 +1,65 @@
+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)
+}