浏览代码

Able to read input

Piotr Czajkowski 1 周之前
父节点
当前提交
46081f2913
共有 1 个文件被更改,包括 50 次插入0 次删除
  1. 50 0
      14/code.go

+ 50 - 0
14/code.go

@@ -0,0 +1,50 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"log"
+	"os"
+)
+
+type Robot struct {
+	x, y   int
+	vX, Vy int
+}
+
+func readInput(file *os.File) []Robot {
+	scanner := bufio.NewScanner(file)
+	var robots []Robot
+
+	for scanner.Scan() {
+		line := scanner.Text()
+		if line == "" {
+			break
+		}
+
+		var robot Robot
+		n, err := fmt.Sscanf(line, "p=%d,%d v=%d,%d", &robot.x, &robot.y, &robot.vX, &robot.Vy)
+		if n != 4 || err != nil {
+			log.Fatalf("Not able to parse robot '%s': %s", line, err)
+		}
+
+		robots = append(robots, robot)
+	}
+
+	return robots
+}
+
+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)
+	}
+
+	robots := readInput(file)
+	fmt.Println(robots)
+}