Browse Source

Able to read input

Piotr Czajkowski 2 years ago
parent
commit
a542d089bd
2 changed files with 101 additions and 0 deletions
  1. 80 0
      13/code.go
  2. 21 0
      13/testinput

+ 80 - 0
13/code.go

@@ -0,0 +1,80 @@
+package main
+
+import (
+	"fmt"
+	"io/ioutil"
+	"log"
+	"os"
+	"strconv"
+	"strings"
+)
+
+type point struct {
+	x, y int
+}
+
+func readInput(file string) ([]point, int, int) {
+	content, err := ioutil.ReadFile(file)
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	lines := strings.Split(string(content), "\n")
+	var input []point
+	var foldX, foldY int
+	readingPoints := true
+	for _, line := range lines {
+		if line == "" {
+			readingPoints = false
+			continue
+		}
+		if readingPoints {
+			parts := strings.Split(line, ",")
+			if len(parts) != 2 {
+				log.Fatal("Invalid input")
+			}
+
+			x, err := strconv.Atoi(parts[0])
+			if err != nil {
+				log.Fatal(err)
+			}
+
+			y, err := strconv.Atoi(parts[1])
+			if err != nil {
+				log.Fatal(err)
+			}
+
+			input = append(input, point{x, y})
+		} else {
+			parts := strings.Split(line, "=")
+			if len(parts) != 2 {
+				log.Fatal("Invalid input")
+			}
+
+			if parts[0] == "fold along x" {
+				foldX, err = strconv.Atoi(parts[1])
+				if err != nil {
+					log.Fatal(err)
+				}
+			} else if parts[0] == "fold along y" {
+				foldY, err = strconv.Atoi(parts[1])
+				if err != nil {
+					log.Fatal(err)
+				}
+			} else {
+				log.Fatal("Invalid input")
+			}
+		}
+	}
+
+	return input, foldX, foldY
+}
+
+func main() {
+	if len(os.Args) < 2 {
+		log.Fatal("Please provide a file name as argument")
+	}
+
+	input, foldX, foldY := readInput(os.Args[1])
+	fmt.Println(input, foldX, foldY)
+}

+ 21 - 0
13/testinput

@@ -0,0 +1,21 @@
+6,10
+0,14
+9,10
+0,3
+10,4
+4,11
+6,0
+6,12
+4,1
+0,13
+10,12
+3,4
+3,0
+8,4
+1,10
+2,14
+8,10
+9,0
+
+fold along y=7
+fold along x=5