day12.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. const (
  9. numberOfDirecrtions = 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. func getDirections() {
  38. directions = []string{"E", "S", "W", "N"}
  39. directionsMap = make(map[string]int)
  40. for index, value := range directions {
  41. directionsMap[value] = index
  42. }
  43. }
  44. func rotate(currentDirection string, item sequence) string {
  45. change := 0
  46. switch item.value {
  47. case 0, 360:
  48. return currentDirection
  49. case 90:
  50. change = 1
  51. case 180:
  52. change = 2
  53. case 270:
  54. change = 3
  55. }
  56. if item.action == "L" {
  57. change = -change
  58. }
  59. newDirectionIndex := (directionsMap[currentDirection] + change) % 4
  60. if newDirectionIndex < 0 {
  61. newDirectionIndex = numberOfDirecrtions + newDirectionIndex
  62. }
  63. return directions[newDirectionIndex]
  64. }
  65. type cooridinates struct {
  66. north int
  67. south int
  68. east int
  69. west int
  70. }
  71. func makeMove(item sequence, position cooridinates) cooridinates {
  72. switch item.action {
  73. case "E":
  74. if position.west > 0 {
  75. position.west = position.west - item.value
  76. if position.west < 0 {
  77. position.east -= position.west
  78. position.west = 0
  79. }
  80. } else {
  81. position.east += item.value
  82. }
  83. case "W":
  84. if position.east > 0 {
  85. position.east = position.east - item.value
  86. if position.east < 0 {
  87. position.west -= position.east
  88. position.east = 0
  89. }
  90. } else {
  91. position.west += item.value
  92. }
  93. case "N":
  94. if position.south > 0 {
  95. position.south = position.south - item.value
  96. if position.south < 0 {
  97. position.north -= position.south
  98. position.south = 0
  99. }
  100. } else {
  101. position.north += item.value
  102. }
  103. case "S":
  104. if position.north > 0 {
  105. position.north = position.north - item.value
  106. if position.north < 0 {
  107. position.south -= position.north
  108. position.north = 0
  109. }
  110. } else {
  111. position.south += item.value
  112. }
  113. }
  114. return position
  115. }
  116. func navigate(sequences []sequence) int {
  117. currentDirection := "E"
  118. currentPosition := cooridinates{north: 0, south: 0, east: 0, west: 0}
  119. for _, item := range sequences {
  120. switch item.action {
  121. case "L", "R":
  122. currentDirection = rotate(currentDirection, item)
  123. case "E", "W", "N", "S":
  124. currentPosition = makeMove(item, currentPosition)
  125. case "F":
  126. item.action = currentDirection
  127. currentPosition = makeMove(item, currentPosition)
  128. }
  129. }
  130. return currentPosition.north + currentPosition.south + currentPosition.east + currentPosition.west
  131. }
  132. func init() {
  133. getDirections()
  134. }
  135. func main() {
  136. if len(os.Args) < 2 {
  137. log.Fatal("You need to specify a file!")
  138. }
  139. file, err := os.Open(os.Args[1])
  140. if err != nil {
  141. log.Fatalf("Failed to open %s!\n", os.Args[1])
  142. }
  143. sequences, err := readFile(file)
  144. if err != nil {
  145. log.Fatalf("Can't read sequences!\n%s", err)
  146. }
  147. file.Close()
  148. fmt.Println("Part1:", navigate(sequences))
  149. }