code.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. for y := height - 1; y >= 0; y-- {
  79. for x := range platform[y] {
  80. if platform[y][x] == 'O' {
  81. direction(platform, y, x, height, width)
  82. }
  83. }
  84. }
  85. }
  86. func tiltPlatformFromRight(platform [][]byte, direction func([][]byte, int, int, int, int), height int, width int) {
  87. for y := range platform {
  88. for x := width - 1; x >= 0; x-- {
  89. if platform[y][x] == 'O' {
  90. direction(platform, y, x, height, width)
  91. }
  92. }
  93. }
  94. }
  95. func calculate(platform [][]byte, height int) int {
  96. var result int
  97. for y := range platform {
  98. for x := range platform[y] {
  99. if platform[y][x] == 'O' {
  100. result += height - y
  101. }
  102. }
  103. }
  104. return result
  105. }
  106. func part1(platform [][]byte) int {
  107. height := len(platform)
  108. width := len(platform[0])
  109. tiltPlatform(platform, tiltNorth, height, width)
  110. return calculate(platform, height)
  111. }
  112. func copyPlatform(platform [][]byte) [][]byte {
  113. var newPlatform [][]byte
  114. for y := range platform {
  115. newPlatform = append(newPlatform, platform[y])
  116. }
  117. return newPlatform
  118. }
  119. func part2(platform [][]byte, cycles int) int {
  120. height := len(platform)
  121. width := len(platform[0])
  122. for i := 0; i < cycles; i++ {
  123. tiltPlatform(platform, tiltNorth, height, width)
  124. tiltPlatform(platform, tiltWest, height, width)
  125. tiltPlatformFromBottom(platform, tiltSouth, height, width)
  126. tiltPlatformFromRight(platform, tiltEast, height, width)
  127. }
  128. return calculate(platform, height)
  129. }
  130. func main() {
  131. if len(os.Args) < 2 {
  132. log.Fatal("You need to specify a file!")
  133. }
  134. filePath := os.Args[1]
  135. file, err := os.Open(filePath)
  136. if err != nil {
  137. log.Fatalf("Failed to open %s!\n", filePath)
  138. }
  139. platform := readInput(file)
  140. platformCopy := copyPlatform(platform)
  141. fmt.Println("Part1:", part1(platform))
  142. fmt.Println("Part2:", part2(platformCopy, 1000))
  143. }