code.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 calculate(platform [][]byte, height int) int {
  78. var result int
  79. for y := range platform {
  80. for x := range platform[y] {
  81. if platform[y][x] == 'O' {
  82. result += height - y
  83. }
  84. }
  85. }
  86. return result
  87. }
  88. func part1(platform [][]byte) int {
  89. height := len(platform)
  90. width := len(platform[0])
  91. tiltPlatform(platform, tiltNorth, height, width)
  92. return calculate(platform, height)
  93. }
  94. func main() {
  95. if len(os.Args) < 2 {
  96. log.Fatal("You need to specify a file!")
  97. }
  98. filePath := os.Args[1]
  99. file, err := os.Open(filePath)
  100. if err != nil {
  101. log.Fatalf("Failed to open %s!\n", filePath)
  102. }
  103. platform := readInput(file)
  104. fmt.Println("Part1:", part1(platform))
  105. }