d08.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package main
  2. import "strconv"
  3. import "strings"
  4. import "bufio"
  5. import "fmt"
  6. import "os"
  7. type Screen struct {
  8. Pixels [6][50]bool
  9. }
  10. func NewScreen() Screen {
  11. var this Screen
  12. return this
  13. }
  14. func (this *Screen) CreateRect(x int, y int) {
  15. for i := 0; i < y; i++ {
  16. for j := 0; j < x; j++ {
  17. this.Pixels[i][j] = true
  18. }
  19. }
  20. }
  21. func (this *Screen) RotateRowByOne(rowIndex int) {
  22. tmp := this.Pixels[rowIndex][49]
  23. for i := 49; i > 0; i-- {
  24. this.Pixels[rowIndex][i] = this.Pixels[rowIndex][i -1]
  25. }
  26. this.Pixels[rowIndex][0] = tmp
  27. }
  28. func (this *Screen) RotateRows(rowIndex int, shift int) {
  29. for ; shift > 0; shift -- {
  30. this.RotateRowByOne(rowIndex)
  31. }
  32. }
  33. func (this *Screen) RotateColumnByOne(colIndex int) {
  34. tmp := this.Pixels[5][colIndex]
  35. for i := 5; i > 0; i-- {
  36. this.Pixels[i][colIndex] = this.Pixels[i -1][colIndex]
  37. }
  38. this.Pixels[0][colIndex] = tmp
  39. }
  40. func (this *Screen) RotateColumn(colIndex int, shift int) {
  41. for ; shift > 0; shift -- {
  42. this.RotateColumnByOne(colIndex)
  43. }
  44. }
  45. func (this *Screen) EvalInstr(line string) bool {
  46. if strings.HasPrefix(line, "rect ") {
  47. size := strings.Split(line[5:], "x")
  48. if len(size) != 2 {
  49. return false
  50. }
  51. px, err := strconv.Atoi(size[0])
  52. if err != nil { return false }
  53. py, err := strconv.Atoi(size[1])
  54. if err != nil { return false }
  55. this.CreateRect(px, py)
  56. } else if (strings.HasPrefix(line, "rotate column x=")) {
  57. size := strings.Split(line[16:], " ")
  58. colIndex, err := strconv.Atoi(size[0])
  59. if err != nil { return false }
  60. rotateBy, err := strconv.Atoi(size[2])
  61. if err != nil { return false }
  62. this.RotateColumn(colIndex, rotateBy)
  63. } else if (strings.HasPrefix(line, "rotate row y=")) {
  64. size := strings.Split(line[13:], " ")
  65. rowIndex, err := strconv.Atoi(size[0])
  66. if err != nil { return false }
  67. rotateBy, err := strconv.Atoi(size[2])
  68. if err != nil { return false }
  69. this.RotateRows(rowIndex, rotateBy)
  70. } else {
  71. return false
  72. }
  73. return true
  74. }
  75. func (this Screen) CountOn() int {
  76. result := 0
  77. for i := 0; i < 6; i++ {
  78. for j := 0; j < 50; j++ {
  79. if this.Pixels[i][j] {
  80. result++
  81. }
  82. }
  83. }
  84. return result
  85. }
  86. func (this Screen) Dump() {
  87. for i := 0; i < 6; i++ {
  88. for j := 0; j < 50; j++ {
  89. if this.Pixels[i][j] {
  90. fmt.Printf("#")
  91. } else {
  92. fmt.Printf(".")
  93. }
  94. }
  95. fmt.Printf("\n")
  96. }
  97. }
  98. func main() {
  99. reader := bufio.NewReader(os.Stdin)
  100. scr := NewScreen()
  101. for true {
  102. line, err := reader.ReadString('\n')
  103. if len(line) > 0 {
  104. line = line[0: len(line) -1]
  105. if scr.EvalInstr(line) == false {
  106. fmt.Println("WTF instruction " +line)
  107. continue
  108. }
  109. }
  110. if (err != nil) {
  111. break
  112. }
  113. }
  114. scr.Dump()
  115. fmt.Println(scr.CountOn())
  116. }