Browse Source

Able to read input

Piotr Czajkowski 2 years ago
parent
commit
7e186ceb10
2 changed files with 78 additions and 0 deletions
  1. 77 0
      17/code.go
  2. 1 0
      17/testinput

+ 77 - 0
17/code.go

@@ -0,0 +1,77 @@
+package main
+
+import (
+	"fmt"
+	"io/ioutil"
+	"log"
+	"os"
+	"strconv"
+	"strings"
+)
+
+type point struct {
+	min, max int
+}
+
+func readInput(file string) []point {
+	content, err := ioutil.ReadFile(file)
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	var points []point
+	line := string(content)
+	initialSplit := strings.Split(line, ", ")
+	if len(initialSplit) != 2 {
+		log.Fatal("Invalid input")
+	}
+
+	firstPartSplit := strings.Split(initialSplit[0], "=")
+	if len(firstPartSplit) != 2 {
+		log.Fatal("Invalid input")
+	}
+
+	firstPartNumbers := strings.Split(firstPartSplit[1], "..")
+	if len(firstPartNumbers) != 2 {
+		log.Fatal("Invalid input")
+	}
+
+	minX, err := strconv.Atoi(firstPartNumbers[0])
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	maxX, err := strconv.Atoi(firstPartNumbers[1])
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	points = append(points, point{minX, maxX})
+
+	secondPartNumbers := strings.Split(initialSplit[1], "..")
+	if len(secondPartNumbers) != 2 {
+		log.Fatal("Invalid input")
+	}
+
+	minY, err := strconv.Atoi(strings.TrimLeft(secondPartNumbers[0], "y="))
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	maxY, err := strconv.Atoi(secondPartNumbers[1])
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	points = append(points, point{minY, maxY})
+	return points
+}
+
+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
17/testinput

@@ -0,0 +1 @@
+target area: x=20..30, y=-10..-5