day12.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 map[string]int
  66. func makeMove(item sequence, position cooridinates) cooridinates {
  67. switch item.action {
  68. case "E":
  69. if position["W"] > 0 {
  70. position["W"] = position["W"] - item.value
  71. if position["W"] < 0 {
  72. position["E"] -= position["W"]
  73. position["W"] = 0
  74. }
  75. } else {
  76. position["E"] += item.value
  77. }
  78. case "W":
  79. if position["E"] > 0 {
  80. position["E"] = position["E"] - item.value
  81. if position["E"] < 0 {
  82. position["W"] -= position["E"]
  83. position["E"] = 0
  84. }
  85. } else {
  86. position["W"] += item.value
  87. }
  88. case "N":
  89. if position["S"] > 0 {
  90. position["S"] = position["S"] - item.value
  91. if position["S"] < 0 {
  92. position["N"] -= position["S"]
  93. position["S"] = 0
  94. }
  95. } else {
  96. position["N"] += item.value
  97. }
  98. case "S":
  99. if position["N"] > 0 {
  100. position["N"] = position["N"] - item.value
  101. if position["N"] < 0 {
  102. position["S"] -= position["N"]
  103. position["N"] = 0
  104. }
  105. } else {
  106. position["S"] += item.value
  107. }
  108. }
  109. return position
  110. }
  111. func navigate(sequences []sequence) int {
  112. currentDirection := "E"
  113. currentPosition := make(cooridinates)
  114. currentPosition["E"] = 0
  115. currentPosition["W"] = 0
  116. currentPosition["N"] = 0
  117. currentPosition["S"] = 0
  118. for _, item := range sequences {
  119. switch item.action {
  120. case "L", "R":
  121. currentDirection = rotate(currentDirection, item)
  122. case "E", "W", "N", "S":
  123. currentPosition = makeMove(item, currentPosition)
  124. case "F":
  125. item.action = currentDirection
  126. currentPosition = makeMove(item, currentPosition)
  127. }
  128. }
  129. sum := 0
  130. for _, value := range currentPosition {
  131. sum += value
  132. }
  133. return sum
  134. }
  135. func rotateWaypoint(waypoint cooridinates) cooridinates {
  136. return waypoint
  137. }
  138. func navigate2(sequences []sequence) int {
  139. currentPosition := make(cooridinates)
  140. currentPosition["E"] = 0
  141. currentPosition["W"] = 0
  142. currentPosition["N"] = 0
  143. currentPosition["S"] = 0
  144. //currentWaypoint := cooridinates{north: 1, south: 0, east: 10, west: 0}
  145. for _, item := range sequences {
  146. switch item.action {
  147. case "L", "R":
  148. //currentDirection = rotate(currentDirection, item)
  149. case "E", "W", "N", "S":
  150. //currentPosition = makeMove(item, currentPosition)
  151. case "F":
  152. //item.action = currentDirection
  153. //currentPosition = makeMove(item, currentPosition)
  154. }
  155. }
  156. sum := 0
  157. for _, value := range currentPosition {
  158. sum += value
  159. }
  160. return sum
  161. }
  162. func init() {
  163. getDirections()
  164. }
  165. func main() {
  166. if len(os.Args) < 2 {
  167. log.Fatal("You need to specify a file!")
  168. }
  169. file, err := os.Open(os.Args[1])
  170. if err != nil {
  171. log.Fatalf("Failed to open %s!\n", os.Args[1])
  172. }
  173. sequences, err := readFile(file)
  174. if err != nil {
  175. log.Fatalf("Can't read sequences!\n%s", err)
  176. }
  177. file.Close()
  178. fmt.Println("Part1:", navigate(sequences))
  179. }