Quellcode durchsuchen

Able to read input

Piotr Czajkowski vor 1 Woche
Ursprung
Commit
54feaffda2
1 geänderte Dateien mit 49 neuen und 0 gelöschten Zeilen
  1. 49 0
      09/code.go

+ 49 - 0
09/code.go

@@ -0,0 +1,49 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"log"
+	"os"
+)
+
+type tile struct {
+	x, y int
+}
+
+func readInput(file *os.File) []tile {
+	scanner := bufio.NewScanner(file)
+	var tiles []tile
+
+	for scanner.Scan() {
+		line := scanner.Text()
+		if line == "" {
+			continue
+		}
+
+		var x, y int
+		n, err := fmt.Sscanf(line, "%d,%d", &x, &y)
+		if n != 2 || err != nil {
+			log.Fatalf("Bad input: %s", line)
+		}
+
+		tiles = append(tiles, tile{x: x, y: y})
+	}
+
+	return tiles
+}
+
+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)
+	}
+
+	tiles := readInput(file)
+	fmt.Println(tiles)
+}