code.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. func readInput(file *os.File) [][]byte {
  9. scanner := bufio.NewScanner(file)
  10. var platform [][]byte
  11. for scanner.Scan() {
  12. line := scanner.Text()
  13. if line == "" {
  14. break
  15. }
  16. var row []byte
  17. for i := range line {
  18. row = append(row, line[i])
  19. }
  20. platform = append(platform, row)
  21. }
  22. return platform
  23. }
  24. func tiltNorth(platform [][]byte, y int, x int, height int, width int) {
  25. for {
  26. prevY := y - 1
  27. if prevY < 0 || platform[prevY][x] == '#' || platform[prevY][x] == 'O' {
  28. break
  29. }
  30. platform[y][x] = '.'
  31. platform[prevY][x] = 'O'
  32. y--
  33. }
  34. }
  35. func tiltSouth(platform [][]byte, y int, x int, height int, width int) {
  36. for {
  37. nextY := y + 1
  38. if nextY >= height || platform[nextY][x] == '#' || platform[nextY][x] == 'O' {
  39. break
  40. }
  41. platform[y][x] = '.'
  42. platform[nextY][x] = 'O'
  43. y++
  44. }
  45. }
  46. func tiltEast(platform [][]byte, y int, x int, height int, width int) {
  47. for {
  48. nextX := x + 1
  49. if nextX >= width || platform[y][nextX] == '#' || platform[y][nextX] == 'O' {
  50. break
  51. }
  52. platform[y][x] = '.'
  53. platform[y][nextX] = 'O'
  54. x++
  55. }
  56. }
  57. func tiltWest(platform [][]byte, y int, x int, height int, width int) {
  58. for {
  59. nextX := x - 1
  60. if nextX < 0 || platform[y][nextX] == '#' || platform[y][nextX] == 'O' {
  61. break
  62. }
  63. platform[y][x] = '.'
  64. platform[y][nextX] = 'O'
  65. x--
  66. }
  67. }
  68. func tiltPlatform(platform [][]byte, direction func([][]byte, int, int, int, int), height int, width int) {
  69. for y := range platform {
  70. for x := range platform[y] {
  71. if platform[y][x] == 'O' {
  72. direction(platform, y, x, height, width)
  73. }
  74. }
  75. }
  76. }
  77. func tiltPlatformFromBottom(platform [][]byte, direction func([][]byte, int, int, int, int), height int, width int) {
  78. y := height - 1
  79. for ; y >= 0; y-- {
  80. for x := range platform[y] {
  81. if platform[y][x] == 'O' {
  82. direction(platform, y, x, height, width)
  83. }
  84. }
  85. }
  86. }
  87. func tiltPlatformFromRight(platform [][]byte, direction func([][]byte, int, int, int, int), height int, width int) {
  88. for y := range platform {
  89. for x := width - 1; x >= 0; x-- {
  90. if platform[y][x] == 'O' {
  91. direction(platform, y, x, height, width)
  92. }
  93. }
  94. }
  95. }
  96. func calculate(platform [][]byte, height int) int {
  97. var result int
  98. for y := range platform {
  99. for x := range platform[y] {
  100. if platform[y][x] == 'O' {
  101. result += height - y
  102. }
  103. }
  104. }
  105. return result
  106. }
  107. func part1(platform [][]byte) int {
  108. height := len(platform)
  109. width := len(platform[0])
  110. tiltPlatform(platform, tiltNorth, height, width)
  111. return calculate(platform, height)
  112. }
  113. func copyPlatform(platform [][]byte) [][]byte {
  114. var newPlatform [][]byte
  115. for y := range platform {
  116. newPlatform = append(newPlatform, platform[y])
  117. }
  118. return newPlatform
  119. }
  120. func printPlatform(platform [][]byte) {
  121. for y := range platform {
  122. fmt.Println(string(platform[y]))
  123. }
  124. }
  125. func part2(platform [][]byte, cycles int) int {
  126. height := len(platform)
  127. width := len(platform[0])
  128. var result int
  129. for i := 0; i < cycles; i++ {
  130. tiltPlatform(platform, tiltNorth, height, width)
  131. tiltPlatform(platform, tiltWest, height, width)
  132. tiltPlatformFromBottom(platform, tiltSouth, height, width)
  133. tiltPlatformFromRight(platform, tiltEast, height, width)
  134. fmt.Println(i + 1)
  135. printPlatform(platform)
  136. }
  137. return result
  138. }
  139. func main() {
  140. if len(os.Args) < 2 {
  141. log.Fatal("You need to specify a file!")
  142. }
  143. filePath := os.Args[1]
  144. file, err := os.Open(filePath)
  145. if err != nil {
  146. log.Fatalf("Failed to open %s!\n", filePath)
  147. }
  148. platform := readInput(file)
  149. platformCopy := copyPlatform(platform)
  150. fmt.Println("Part1:", part1(platform))
  151. fmt.Println("Part2:", part2(platformCopy, 4))
  152. }