Browse Source

Able to read input

Piotr Czajkowski 2 years ago
parent
commit
02ac06c4aa
2 changed files with 94 additions and 0 deletions
  1. 90 0
      22/code.go
  2. 4 0
      22/testinput

+ 90 - 0
22/code.go

@@ -0,0 +1,90 @@
+package main
+
+import (
+	"fmt"
+	"io/ioutil"
+	"log"
+	"os"
+	"strconv"
+	"strings"
+)
+
+type action struct {
+	on bool
+	x  []int
+	y  []int
+	z  []int
+}
+
+func readInput(file string) []action {
+	content, err := ioutil.ReadFile(file)
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	lines := strings.Split(string(content), "\n")
+	var input []action
+	for _, line := range lines {
+		if line == "" {
+			continue
+		}
+
+		var a action
+		if strings.HasPrefix(line, "on") {
+			a.on = true
+		}
+
+		trash := strings.Split(line, " ")
+		if len(trash) != 2 {
+			log.Fatal("Invalid input")
+		}
+
+		parts := strings.Split(trash[1], ",")
+		if len(parts) != 3 {
+			log.Fatal("Invalid input")
+		}
+
+		for i, part := range parts {
+			another := strings.Split(part, "=")
+			if len(another) != 2 {
+				log.Fatal("Invalid input")
+			}
+
+			numbers := strings.Split(another[1], "..")
+			if len(numbers) != 2 {
+				log.Fatal("Invalid input")
+			}
+
+			var first, second int
+			if first, err = strconv.Atoi(numbers[0]); err != nil {
+				log.Fatal(err)
+			}
+
+			if second, err = strconv.Atoi(numbers[1]); err != nil {
+				log.Fatal(err)
+			}
+
+			switch i {
+			case 0:
+				a.x = []int{first, second}
+			case 1:
+				a.y = []int{first, second}
+			case 2:
+				a.z = []int{first, second}
+			}
+		}
+
+		input = append(input, a)
+	}
+
+	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)
+}

+ 4 - 0
22/testinput

@@ -0,0 +1,4 @@
+on x=10..12,y=10..12,z=10..12
+on x=11..13,y=11..13,z=11..13
+off x=9..11,y=9..11,z=9..11
+on x=10..10,y=10..10,z=10..10