day12.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. const (
  9. numberOfDirections = 4
  10. )
  11. type sequence struct {
  12. action string
  13. value int
  14. }
  15. func readFile(file *os.File) ([]sequence, error) {
  16. var sequences []sequence
  17. scanner := bufio.NewScanner(file)
  18. for scanner.Scan() {
  19. var p sequence
  20. line := scanner.Text()
  21. if line == "" {
  22. break
  23. }
  24. n, err := fmt.Sscanf(line, "%1s%d\n", &p.action, &p.value)
  25. if err != nil || n != 2 {
  26. return sequences, fmt.Errorf("Error scanning '%s': %s", line, err)
  27. }
  28. sequences = append(sequences, p)
  29. }
  30. if err := scanner.Err(); err != nil {
  31. return sequences, fmt.Errorf("Scanner error: %s", err)
  32. }
  33. return sequences, nil
  34. }
  35. var directions []string
  36. var directionsMap map[string]int
  37. var opposites map[string]string
  38. func getDirections() {
  39. directions = []string{"E", "S", "W", "N"}
  40. directionsMap = make(map[string]int)
  41. for index, value := range directions {
  42. directionsMap[value] = index
  43. }
  44. opposites = make(map[string]string)
  45. opposites["E"] = "W"
  46. opposites["W"] = "E"
  47. opposites["N"] = "S"
  48. opposites["S"] = "N"
  49. }
  50. func rotate(currentDirection string, item sequence) string {
  51. change := 0
  52. switch item.value {
  53. case 0, 360:
  54. return currentDirection
  55. case 90:
  56. change = 1
  57. case 180:
  58. change = 2
  59. case 270:
  60. change = 3
  61. }
  62. if item.action == "L" {
  63. change = -change
  64. }
  65. newDirectionIndex := (directionsMap[currentDirection] + change) % 4
  66. if newDirectionIndex < 0 {
  67. newDirectionIndex = numberOfDirections + newDirectionIndex
  68. }
  69. return directions[newDirectionIndex]
  70. }
  71. type coordinates map[string]int
  72. func makeMove(item sequence, position coordinates) coordinates {
  73. opposite := opposites[item.action]
  74. if position[opposite] > 0 {
  75. position[opposite] = position[opposite] - item.value
  76. if position[opposite] < 0 {
  77. position[item.action] -= position[opposite]
  78. position[opposite] = 0
  79. }
  80. } else {
  81. position[item.action] += item.value
  82. }
  83. return position
  84. }
  85. func navigate(sequences []sequence) int {
  86. currentDirection := "E"
  87. currentPosition := make(coordinates)
  88. currentPosition["E"] = 0
  89. currentPosition["W"] = 0
  90. currentPosition["N"] = 0
  91. currentPosition["S"] = 0
  92. for _, item := range sequences {
  93. switch item.action {
  94. case "L", "R":
  95. currentDirection = rotate(currentDirection, item)
  96. case "E", "W", "N", "S":
  97. currentPosition = makeMove(item, currentPosition)
  98. case "F":
  99. item.action = currentDirection
  100. currentPosition = makeMove(item, currentPosition)
  101. }
  102. }
  103. sum := 0
  104. for _, value := range currentPosition {
  105. sum += value
  106. }
  107. return sum
  108. }
  109. func rotateWaypoint(waypoint coordinates, item sequence) coordinates {
  110. newWaypoint := make(coordinates)
  111. for key, value := range waypoint {
  112. newKey := rotate(key, item)
  113. newWaypoint[newKey] = value
  114. }
  115. return newWaypoint
  116. }
  117. func navigate2(sequences []sequence) int {
  118. currentPosition := make(coordinates)
  119. currentPosition["E"] = 0
  120. currentPosition["W"] = 0
  121. currentPosition["N"] = 0
  122. currentPosition["S"] = 0
  123. currentWaypoint := make(coordinates)
  124. currentWaypoint["E"] = 10
  125. currentWaypoint["W"] = 0
  126. currentWaypoint["N"] = 1
  127. currentWaypoint["S"] = 0
  128. for _, item := range sequences {
  129. switch item.action {
  130. case "L", "R":
  131. currentWaypoint = rotateWaypoint(currentWaypoint, item)
  132. case "E", "W", "N", "S":
  133. currentWaypoint = makeMove(item, currentWaypoint)
  134. case "F":
  135. for key, value := range currentWaypoint {
  136. currentPosition = makeMove(sequence{action: key, value: value * item.value}, currentPosition)
  137. }
  138. }
  139. }
  140. sum := 0
  141. for _, value := range currentPosition {
  142. sum += value
  143. }
  144. return sum
  145. }
  146. func init() {
  147. getDirections()
  148. }
  149. func main() {
  150. if len(os.Args) < 2 {
  151. log.Fatal("You need to specify a file!")
  152. }
  153. file, err := os.Open(os.Args[1])
  154. if err != nil {
  155. log.Fatalf("Failed to open %s!\n", os.Args[1])
  156. }
  157. sequences, err := readFile(file)
  158. if err != nil {
  159. log.Fatalf("Can't read sequences!\n%s", err)
  160. }
  161. if err := file.Close(); err != nil {
  162. log.Fatalf("Failed to close file: %s", err)
  163. }
  164. fmt.Println("Part1:", navigate(sequences))
  165. fmt.Println("Part2:", navigate2(sequences))
  166. }