Browse Source

Able to read input

Piotr Czajkowski 2 years ago
parent
commit
8421fe5186
2 changed files with 87 additions and 0 deletions
  1. 76 0
      24/code.go
  2. 11 0
      24/testinput

+ 76 - 0
24/code.go

@@ -0,0 +1,76 @@
+package main
+
+import (
+	"fmt"
+	"io/ioutil"
+	"log"
+	"os"
+	"strconv"
+	"strings"
+	"unicode"
+)
+
+type operation struct {
+	op        string
+	a         byte
+	isBNumber bool
+	b         int
+	bC        byte
+}
+
+func readInput(file string) [][]operation {
+	content, err := ioutil.ReadFile(file)
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	lines := strings.Split(string(content), "\n")
+	var input [][]operation
+	var operations []operation
+	for _, line := range lines {
+		if line == "" {
+			continue
+		}
+
+		parts := strings.Split(line, " ")
+		if len(parts) < 2 {
+			log.Fatal("Invalid line: ", line)
+		}
+
+		if parts[0] == "inp" && len(operations) > 0 {
+			input = append(input, operations)
+			operations = []operation{}
+		}
+
+		current := operation{op: parts[0], a: parts[1][0]}
+		if len(parts) > 2 {
+			if unicode.IsDigit(rune(parts[2][0])) {
+				current.b, err = strconv.Atoi(parts[2])
+				if err != nil {
+					log.Fatal(err)
+				}
+
+				current.isBNumber = true
+			} else {
+				current.bC = parts[2][0]
+			}
+		}
+
+		operations = append(operations, current)
+	}
+
+	if len(operations) > 0 {
+		input = append(input, operations)
+	}
+
+	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)
+}

+ 11 - 0
24/testinput

@@ -0,0 +1,11 @@
+inp w
+add z w
+mod z 2
+div w 2
+add y w
+mod y 2
+div w 2
+add x w
+mod x 2
+div w 2
+mod w 2