Browse Source

Able to read fish

Piotr Czajkowski 2 years ago
parent
commit
2276c69836
2 changed files with 43 additions and 0 deletions
  1. 42 0
      06/code.go
  2. 1 0
      06/testinput

+ 42 - 0
06/code.go

@@ -0,0 +1,42 @@
+package main
+
+import (
+	"fmt"
+	"io/ioutil"
+	"log"
+	"os"
+	"strconv"
+	"strings"
+)
+
+func readInput(file string) []int {
+	content, err := ioutil.ReadFile(file)
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	fishStrings := strings.Split(string(content), ",")
+	var input []int
+	for _, fishString := range fishStrings {
+		if fishString == "" {
+			continue
+		}
+
+		if number, err := strconv.Atoi(fishString); err == nil {
+			input = append(input, number)
+		} else {
+			log.Fatal(err)
+		}
+	}
+
+	return input
+}
+
+func main() {
+	if len(os.Args) < 2 {
+		log.Fatal("Please provide a file name as argument")
+	}
+
+	input := readInput(os.Args[1])
+	fmt.Println(input)
+}

+ 1 - 0
06/testinput

@@ -0,0 +1 @@
+3,4,3,1,2