Bläddra i källkod

Able to read tickets

Piotr Czajkowski 3 år sedan
förälder
incheckning
9414c98f9f
1 ändrade filer med 40 tillägg och 2 borttagningar
  1. 40 2
      day16/day16.go

+ 40 - 2
day16/day16.go

@@ -5,6 +5,8 @@ import (
 	"fmt"
 	"log"
 	"os"
+	"strconv"
+	"strings"
 )
 
 type rule struct {
@@ -25,21 +27,56 @@ func readRule(line string) {
 	rules = append(rules, newRule)
 }
 
+type ticket []int64
+
+var tickets []ticket
+
+func readTicket(line string) {
+	var newTicket ticket
+	for _, item := range strings.Split(line, ",") {
+		field, err := strconv.ParseInt(item, 10, 32)
+		if err != nil {
+			log.Fatalf("Error parsing field from %s: %s", item, err)
+		}
+
+		newTicket = append(newTicket, field)
+	}
+
+	tickets = append(tickets, newTicket)
+}
+
 func readFile(file *os.File) {
 	scanner := bufio.NewScanner(file)
+	index := 0
 	for scanner.Scan() {
 		line := scanner.Text()
 		if line == "" {
-			break
+			continue
 		}
 
-		readRule(line)
+		if strings.Contains(line, "your ticket:") {
+			index++
+			continue
+		}
+
+		if strings.Contains(line, "nearby tickets:") {
+			continue
+		}
+
+		functions[index](line)
 	}
 	if err := scanner.Err(); err != nil {
 		log.Fatalf("Scanner error: %s", err)
 	}
 }
 
+var functions []func(string)
+
+func init() {
+	functions = append(functions, readRule)
+	functions = append(functions, readTicket)
+}
+
 func main() {
 	if len(os.Args) < 2 {
 		log.Fatal("You need to specify a file!")
@@ -58,4 +95,5 @@ func main() {
 	}
 
 	fmt.Println(rules)
+	fmt.Println(tickets)
 }