isundil 9 жил өмнө
parent
commit
f5155aaadc
1 өөрчлөгдсөн 134 нэмэгдсэн , 0 устгасан
  1. 134 0
      d08/d08.go

+ 134 - 0
d08/d08.go

@@ -0,0 +1,134 @@
+package main
+
+import "strconv"
+import "strings"
+import "bufio"
+import "fmt"
+import "os"
+
+type Screen struct {
+    Pixels [6][50]bool
+}
+
+func NewScreen() Screen {
+    var this Screen
+    return this
+}
+
+func (this *Screen) CreateRect(x int, y int) {
+    for i := 0; i < y; i++ {
+        for j := 0; j < x; j++ {
+            this.Pixels[i][j] = true
+        }
+    }
+}
+
+func (this *Screen) RotateRowByOne(rowIndex int) {
+    tmp := this.Pixels[rowIndex][49]
+    for i := 49; i > 0; i-- {
+        this.Pixels[rowIndex][i] = this.Pixels[rowIndex][i -1]
+    }
+    this.Pixels[rowIndex][0] = tmp
+}
+
+func (this *Screen) RotateRows(rowIndex int, shift int) {
+    for ; shift > 0; shift -- {
+        this.RotateRowByOne(rowIndex)
+    }
+}
+
+func (this *Screen) RotateColumnByOne(colIndex int) {
+    tmp := this.Pixels[5][colIndex]
+    for i := 5; i > 0; i-- {
+        this.Pixels[i][colIndex] = this.Pixels[i -1][colIndex]
+    }
+    this.Pixels[0][colIndex] = tmp
+}
+
+func (this *Screen) RotateColumn(colIndex int, shift int) {
+    for ; shift > 0; shift -- {
+        this.RotateColumnByOne(colIndex)
+    }
+}
+
+func (this *Screen) EvalInstr(line string) bool {
+    if strings.HasPrefix(line, "rect ") {
+        size := strings.Split(line[5:], "x")
+        if len(size) != 2 {
+            return false
+        }
+        px, err := strconv.Atoi(size[0])
+        if err != nil { return false }
+        py, err := strconv.Atoi(size[1])
+        if err != nil { return false }
+        this.CreateRect(px, py)
+
+    } else if (strings.HasPrefix(line, "rotate column x=")) {
+        size := strings.Split(line[16:], " ")
+        colIndex, err := strconv.Atoi(size[0])
+        if err != nil { return false }
+        rotateBy, err := strconv.Atoi(size[2])
+        if err != nil { return false }
+        this.RotateColumn(colIndex, rotateBy)
+
+    } else if (strings.HasPrefix(line, "rotate row y=")) {
+        size := strings.Split(line[13:], " ")
+        rowIndex, err := strconv.Atoi(size[0])
+        if err != nil { return false }
+        rotateBy, err := strconv.Atoi(size[2])
+        if err != nil { return false }
+        this.RotateRows(rowIndex, rotateBy)
+
+    } else {
+        return false
+    }
+    return true
+}
+
+func (this Screen) CountOn() int {
+    result := 0
+
+    for i := 0; i < 6; i++ {
+        for j := 0; j < 50; j++ {
+            if this.Pixels[i][j] {
+                result++
+            }
+        }
+    }
+    return result
+}
+
+func (this Screen) Dump() {
+    for i := 0; i < 6; i++ {
+        for j := 0; j < 50; j++ {
+            if this.Pixels[i][j] {
+                fmt.Printf("#")
+            } else {
+                fmt.Printf(".")
+            }
+        }
+        fmt.Printf("\n")
+    }
+}
+
+func main() {
+    reader := bufio.NewReader(os.Stdin)
+    scr := NewScreen()
+
+    for true {
+        line, err := reader.ReadString('\n')
+        if len(line) > 0 {
+            line = line[0: len(line) -1]
+            if scr.EvalInstr(line) == false {
+                fmt.Println("WTF instruction " +line)
+                continue
+            }
+        }
+        if (err != nil) {
+            break
+        }
+    }
+    scr.Dump()
+    fmt.Println(scr.CountOn())
+}
+