isundil 9 years ago
parent
commit
7ce2a3e19e
1 changed files with 72 additions and 0 deletions
  1. 72 0
      src/d02/d02.go

+ 72 - 0
src/d02/d02.go

@@ -0,0 +1,72 @@
+package main
+
+import "bufio"
+import "fmt"
+import "os"
+
+type Digicode struct {
+    mat [][]string
+    result string
+    pos []int
+}
+
+func NewDigicode() Digicode {
+    var res Digicode
+    /** First case */
+    /*
+    res.pos = []int { 1, 1 }
+    res.mat = append(res.mat, []string{"1", "2", "3"})
+    res.mat = append(res.mat, []string{"4", "5", "6"})
+    res.mat = append(res.mat, []string{"7", "8", "9"})
+    */
+
+    /** second case */
+    res.pos = []int { 2, 0 }
+    res.mat = append(res.mat, []string{".", ".", "1", ".", "."})
+    res.mat = append(res.mat, []string{".", "2", "3", "4", "."})
+    res.mat = append(res.mat, []string{"5", "6", "7", "8", "9"})
+    res.mat = append(res.mat, []string{".", "A", "B", "C", "."})
+    res.mat = append(res.mat, []string{".", ".", "D", ".", "."})
+    return res
+}
+
+func (this *Digicode) NextDigit(line string) {
+    code := make(map[rune][]int)
+
+    code['U'] = []int { -1, 0  }
+    code['D'] = []int {  1, 0  }
+    code['L'] = []int {  0, -1 }
+    code['R'] = []int {  0, 1  }
+
+    for _, dir := range(line) {
+        if (this.pos[0] + code[dir][0] < 0 ||
+            this.pos[1] + code[dir][1] < 0 ||
+            this.pos[0] + code[dir][0] >= len(this.mat) ||
+            this.pos[1] + code[dir][1] >= len(this.mat[this.pos[0] + code[dir][0]])) {
+            continue
+        }
+        newCode := []int { this.pos[0] + code[dir][0], this.pos[1] + code[dir][1]}
+        if (this.mat[newCode[0]][newCode[1]] == ".") {
+            continue
+        }
+        this.pos = newCode
+    }
+    this.result += this.mat[this.pos[0]][this.pos[1]]
+}
+
+func main() {
+    reader := bufio.NewReader(os.Stdin)
+    code := NewDigicode()
+
+    for true {
+        read, _, err := reader.ReadLine()
+        if len(read) > 0 {
+            code.NextDigit(string(read))
+        }
+        if (err != nil) {
+            break
+        }
+    }
+
+    fmt.Println("output: ", code.result)
+}