瀏覽代碼

Able to read operation

Piotr Czajkowski 2 年之前
父節點
當前提交
7ed3c2ecaf
共有 1 個文件被更改,包括 53 次插入0 次删除
  1. 53 0
      11/code.go

+ 53 - 0
11/code.go

@@ -9,8 +9,27 @@ import (
 	"strings"
 )
 
+type kind byte
+
+const (
+	old kind = iota
+	val
+)
+
+type variable struct {
+	t   kind
+	val int
+}
+
+type operation struct {
+	x      variable
+	y      variable
+	action byte
+}
+
 type monkey struct {
 	items []int
+	op    operation
 }
 
 func readItems(line string) []int {
@@ -30,6 +49,38 @@ func readItems(line string) []int {
 	return result
 }
 
+func readVariable(text string) variable {
+	var result variable
+	if text == "old" {
+		result.t = old
+	} else {
+		result.t = val
+		n, err := strconv.Atoi(text)
+		if err != nil {
+			log.Fatal("Can't pasrse", text, err)
+		}
+
+		result.val = n
+	}
+
+	return result
+}
+
+func readOperation(line string) operation {
+	line = strings.Replace(line, "  Operation: new = ", "", 1)
+	parts := strings.Split(line, " ")
+	if len(parts) != 3 {
+		log.Fatal("Bad operation input:", line)
+	}
+
+	var result operation
+	result.x = readVariable(parts[0])
+	result.y = readVariable(parts[2])
+	result.action = parts[1][0]
+
+	return result
+}
+
 func readInput(file *os.File) []monkey {
 	scanner := bufio.NewScanner(file)
 	counter := 0
@@ -48,6 +99,8 @@ func readInput(file *os.File) []monkey {
 		switch counter {
 		case 1:
 			currentMonkey.items = readItems(line)
+		case 2:
+			currentMonkey.op = readOperation(line)
 		}
 		counter++
 	}