day24.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. func readDirections(line string) []string {
  9. var path []string
  10. signs := []rune(line)
  11. size := len(signs)
  12. for i := 0; i < size; i++ {
  13. switch signs[i] {
  14. case 'e', 'w':
  15. path = append(path, string(signs[i]))
  16. case 's', 'n':
  17. path = append(path, string(signs[i:i+2]))
  18. i++
  19. }
  20. }
  21. return path
  22. }
  23. func readFile(file *os.File) [][]string {
  24. var paths [][]string
  25. scanner := bufio.NewScanner(file)
  26. for scanner.Scan() {
  27. line := scanner.Text()
  28. if line == "" {
  29. break
  30. }
  31. paths = append(paths, readDirections(line))
  32. }
  33. if err := scanner.Err(); err != nil {
  34. log.Fatalf("Scanner error: %s", err)
  35. }
  36. return paths
  37. }
  38. type position struct {
  39. x int
  40. y int
  41. }
  42. func makeMove(path []string) position {
  43. currentPosition := position{x: 0, y: 0}
  44. for _, item := range path {
  45. switch item {
  46. case "e":
  47. currentPosition.x += 2
  48. case "w":
  49. currentPosition.x -= 2
  50. case "se":
  51. currentPosition.x += 1
  52. currentPosition.y -= 1
  53. case "sw":
  54. currentPosition.x -= 1
  55. currentPosition.y -= 1
  56. case "nw":
  57. currentPosition.x -= 1
  58. currentPosition.y += 1
  59. case "ne":
  60. currentPosition.x += 1
  61. currentPosition.y += 1
  62. }
  63. }
  64. return currentPosition
  65. }
  66. func makeAllMoves(paths [][]string) map[position]int {
  67. moves := make(map[position]int)
  68. for _, path := range paths {
  69. currentPosition := makeMove(path)
  70. moves[currentPosition] += 1
  71. }
  72. return moves
  73. }
  74. func part1(tiles map[position]int) map[position]int {
  75. blackTiles := make(map[position]int)
  76. for key, value := range tiles {
  77. if value%2 != 0 {
  78. blackTiles[key] = 1
  79. }
  80. }
  81. return blackTiles
  82. }
  83. func findNeighbours(tile position) map[position]int {
  84. neighbours := make(map[position]int)
  85. neighbours[position{x: tile.x - 2, y: tile.y}] = 0
  86. neighbours[position{x: tile.x + 2, y: tile.y}] = 0
  87. neighbours[position{x: tile.x + 1, y: tile.y - 1}] = 0
  88. neighbours[position{x: tile.x - 1, y: tile.y - 1}] = 0
  89. neighbours[position{x: tile.x - 1, y: tile.y + 1}] = 0
  90. neighbours[position{x: tile.x + 1, y: tile.y + 1}] = 0
  91. return neighbours
  92. }
  93. func numberOfBlackNeighbours(neighbours map[position]int, blackTiles map[position]int) int {
  94. black := 0
  95. for neighbour, _ := range neighbours {
  96. if _, ok := blackTiles[neighbour]; ok {
  97. black++
  98. }
  99. }
  100. return black
  101. }
  102. func flip(blackTiles map[position]int) map[position]int {
  103. newBlackTiles := make(map[position]int)
  104. for key, _ := range blackTiles {
  105. neighbours := findNeighbours(key)
  106. blackNeighbours := numberOfBlackNeighbours(neighbours, blackTiles)
  107. if blackNeighbours > 0 && blackNeighbours <= 2 {
  108. newBlackTiles[key] = 0
  109. }
  110. for neighbour, _ := range neighbours {
  111. if _, ok := blackTiles[neighbour]; ok {
  112. continue
  113. }
  114. anotherNeighbours := findNeighbours(neighbour)
  115. if numberOfBlackNeighbours(anotherNeighbours, blackTiles) == 2 {
  116. newBlackTiles[neighbour] = 0
  117. }
  118. }
  119. }
  120. return newBlackTiles
  121. }
  122. func do100Flips(blackTiles map[position]int) int {
  123. for i := 0; i < 100; i++ {
  124. blackTiles = flip(blackTiles)
  125. }
  126. return len(blackTiles)
  127. }
  128. func main() {
  129. if len(os.Args) < 2 {
  130. log.Fatal("You need to specify a file!")
  131. }
  132. filePath := os.Args[1]
  133. file, err := os.Open(filePath)
  134. if err != nil {
  135. log.Fatalf("Failed to open %s!\n", filePath)
  136. }
  137. paths := readFile(file)
  138. if err := file.Close(); err != nil {
  139. log.Fatalf("Failed to close file: %s", err)
  140. }
  141. tiles := makeAllMoves(paths)
  142. blackTiles := part1(tiles)
  143. fmt.Println("Part1:", len(blackTiles))
  144. fmt.Println("Part2:", do100Flips(blackTiles))
  145. }