Browse Source

Able to read input

Piotr Czajkowski 2 years ago
parent
commit
ff3a2a9e57
2 changed files with 52 additions and 0 deletions
  1. 47 0
      09/code.go
  2. 5 0
      09/testinput

+ 47 - 0
09/code.go

@@ -0,0 +1,47 @@
+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)
+	}
+
+	lines := strings.Split(string(content), "\n")
+	var input [][]int
+	for _, line := range lines {
+		if line == "" {
+			continue
+		}
+
+		var numbers []int
+		for _, letter := range line {
+			if number, err := strconv.Atoi(string(letter)); err == nil {
+				numbers = append(numbers, number)
+			} else {
+				log.Fatal(err)
+			}
+		}
+
+		input = append(input, numbers)
+	}
+
+	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)
+}

+ 5 - 0
09/testinput

@@ -0,0 +1,5 @@
+2199943210
+3987894921
+9856789892
+8767896789
+9899965678