code.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 moveO(a *byte, b *byte) {
  25. *a = '.'
  26. *b = 'O'
  27. }
  28. func tiltNorth(platform [][]byte, y int, x int, height int, width int) {
  29. for {
  30. prevY := y - 1
  31. if prevY < 0 || platform[prevY][x] == '#' || platform[prevY][x] == 'O' {
  32. break
  33. }
  34. moveO(&platform[y][x], &platform[prevY][x])
  35. y--
  36. }
  37. }
  38. func tiltSouth(platform [][]byte, y int, x int, height int, width int) {
  39. for {
  40. nextY := y + 1
  41. if nextY >= height || platform[nextY][x] == '#' || platform[nextY][x] == 'O' {
  42. break
  43. }
  44. moveO(&platform[y][x], &platform[nextY][x])
  45. y++
  46. }
  47. }
  48. func tiltEast(platform [][]byte, y int, x int, height int, width int) {
  49. for {
  50. nextX := x + 1
  51. if nextX >= width || platform[y][nextX] == '#' || platform[y][nextX] == 'O' {
  52. break
  53. }
  54. moveO(&platform[y][x], &platform[y][nextX])
  55. x++
  56. }
  57. }
  58. func tiltWest(platform [][]byte, y int, x int, height int, width int) {
  59. for {
  60. nextX := x - 1
  61. if nextX < 0 || platform[y][nextX] == '#' || platform[y][nextX] == 'O' {
  62. break
  63. }
  64. moveO(&platform[y][x], &platform[y][nextX])
  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 tiltPlatformFromBottomRight(platform [][]byte, direction func([][]byte, int, int, int, int), height int, width int) {
  78. for y := height - 1; y >= 0; y-- {
  79. for x := width - 1; x >= 0; x-- {
  80. if platform[y][x] == 'O' {
  81. direction(platform, y, x, height, width)
  82. }
  83. }
  84. }
  85. }
  86. func calculate(platform [][]byte, height int) int {
  87. var result int
  88. for y := range platform {
  89. for x := range platform[y] {
  90. if platform[y][x] == 'O' {
  91. result += height - y
  92. }
  93. }
  94. }
  95. return result
  96. }
  97. func part1(platform [][]byte) int {
  98. height := len(platform)
  99. width := len(platform[0])
  100. tiltPlatform(platform, tiltNorth, height, width)
  101. return calculate(platform, height)
  102. }
  103. func copyPlatform(platform [][]byte) [][]byte {
  104. var newPlatform [][]byte
  105. for y := range platform {
  106. newPlatform = append(newPlatform, platform[y])
  107. }
  108. return newPlatform
  109. }
  110. func part2(platform [][]byte, cycles int) int {
  111. height := len(platform)
  112. width := len(platform[0])
  113. for i := 0; i < cycles; i++ {
  114. tiltPlatform(platform, tiltNorth, height, width)
  115. tiltPlatform(platform, tiltWest, height, width)
  116. tiltPlatformFromBottomRight(platform, tiltSouth, height, width)
  117. tiltPlatformFromBottomRight(platform, tiltEast, height, width)
  118. }
  119. return calculate(platform, height)
  120. }
  121. func main() {
  122. if len(os.Args) < 2 {
  123. log.Fatal("You need to specify a file!")
  124. }
  125. filePath := os.Args[1]
  126. file, err := os.Open(filePath)
  127. if err != nil {
  128. log.Fatalf("Failed to open %s!\n", filePath)
  129. }
  130. platform := readInput(file)
  131. platformCopy := copyPlatform(platform)
  132. fmt.Println("Part1:", part1(platform))
  133. fmt.Println("Part2:", part2(platformCopy, 1000))
  134. }