Browse Source

Able to read input

Piotr Czajkowski 2 years ago
parent
commit
50c3beec95
2 changed files with 59 additions and 0 deletions
  1. 53 0
      02/code.go
  2. 6 0
      02/testinput

+ 53 - 0
02/code.go

@@ -0,0 +1,53 @@
+package main
+
+import (
+	"fmt"
+	"io/ioutil"
+	"log"
+	"os"
+	"strings"
+)
+
+type move struct {
+	direction string
+	steps     int
+}
+
+func readInput(file string) []move {
+	var moves []move
+
+	content, err := ioutil.ReadFile(file)
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	lines := strings.Split(string(content), "\n")
+	for _, line := range lines {
+		if line == "" {
+			continue
+		}
+
+		var currentMove move
+		n, err := fmt.Sscanf(line, "%s %d", &currentMove.direction, &currentMove.steps)
+		if err != nil {
+			log.Fatal(err)
+		}
+
+		if n != 2 {
+			continue
+		}
+
+		moves = append(moves, currentMove)
+	}
+
+	return moves
+}
+
+func main() {
+	if len(os.Args) < 2 {
+		log.Fatal("No input file specified")
+	}
+
+	moves := readInput(os.Args[1])
+	fmt.Println(moves)
+}

+ 6 - 0
02/testinput

@@ -0,0 +1,6 @@
+forward 5
+down 5
+forward 8
+up 3
+down 8
+forward 2