day24.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 main() {
  94. if len(os.Args) < 2 {
  95. log.Fatal("You need to specify a file!")
  96. }
  97. filePath := os.Args[1]
  98. file, err := os.Open(filePath)
  99. if err != nil {
  100. log.Fatalf("Failed to open %s!\n", filePath)
  101. }
  102. paths := readFile(file)
  103. if err := file.Close(); err != nil {
  104. log.Fatalf("Failed to close file: %s", err)
  105. }
  106. tiles := makeAllMoves(paths)
  107. blackTiles := part1(tiles)
  108. fmt.Println("Part1:", len(blackTiles))
  109. for key, _ := range blackTiles {
  110. fmt.Println(findNeighbours(key))
  111. }
  112. }