Browse Source

Able to read input

Piotr Czajkowski 2 years ago
parent
commit
2d82575a40
2 changed files with 53 additions and 0 deletions
  1. 43 0
      12/code.go
  2. 10 0
      12/testinput

+ 43 - 0
12/code.go

@@ -0,0 +1,43 @@
+package main
+
+import (
+	"fmt"
+	"io/ioutil"
+	"log"
+	"os"
+	"strings"
+)
+
+func readInput(file string) [][]string {
+	content, err := ioutil.ReadFile(file)
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	lines := strings.Split(string(content), "\n")
+	var input [][]string
+	for _, line := range lines {
+		if line == "" {
+			continue
+		}
+
+		points := strings.Split(line, "-")
+		if len(points) != 2 {
+			log.Fatal("Invalid input")
+		}
+
+		input = append(input, []string{points[0], points[1]})
+		input = append(input, []string{points[1], points[0]})
+	}
+
+	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)
+}

+ 10 - 0
12/testinput

@@ -0,0 +1,10 @@
+dc-end
+HN-start
+start-kj
+dc-start
+dc-HN
+LN-dc
+HN-end
+kj-sa
+kj-HN
+kj-dc