code.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. type move struct {
  9. direction byte
  10. steps int
  11. }
  12. func readInput(file *os.File) []move {
  13. scanner := bufio.NewScanner(file)
  14. var moves []move
  15. for scanner.Scan() {
  16. line := scanner.Text()
  17. if line == "" {
  18. continue
  19. }
  20. var current move
  21. n, err := fmt.Sscanf(line, "%c %d", &current.direction, &current.steps)
  22. if n != 2 || err != nil {
  23. log.Fatal("Can't parse cd:", err)
  24. }
  25. moves = append(moves, current)
  26. }
  27. return moves
  28. }
  29. type point struct {
  30. x int
  31. y int
  32. }
  33. func tailInVicinity(leader *point, tail point) bool {
  34. if leader == nil {
  35. return false
  36. }
  37. return tail.x >= leader.x-1 && tail.x <= leader.x+1 && tail.y >= leader.y-1 && tail.y <= leader.y+1
  38. }
  39. func catchUp(leader *point, tail point) point {
  40. if tail.x > leader.x {
  41. tail.x--
  42. } else if tail.x < leader.x {
  43. tail.x++
  44. }
  45. if tail.y > leader.y {
  46. tail.y--
  47. } else if tail.y < leader.y {
  48. tail.y++
  49. }
  50. return tail
  51. }
  52. func moveRight(leader *point, tail point) point {
  53. if !tailInVicinity(leader, tail) {
  54. if leader != nil {
  55. tail = catchUp(leader, tail)
  56. } else {
  57. tail.x++
  58. }
  59. }
  60. return tail
  61. }
  62. func moveLeft(leader *point, tail point) point {
  63. if !tailInVicinity(leader, tail) {
  64. if leader != nil {
  65. tail = catchUp(leader, tail)
  66. } else {
  67. tail.x--
  68. }
  69. }
  70. return tail
  71. }
  72. func moveUp(leader *point, tail point) point {
  73. if !tailInVicinity(leader, tail) {
  74. if leader != nil {
  75. tail = catchUp(leader, tail)
  76. } else {
  77. tail.y++
  78. }
  79. }
  80. return tail
  81. }
  82. func moveDown(leader *point, tail point) point {
  83. if !tailInVicinity(leader, tail) {
  84. if leader != nil {
  85. tail = catchUp(leader, tail)
  86. } else {
  87. tail.y--
  88. }
  89. }
  90. return tail
  91. }
  92. func drawTail(leader *point, tail point, action move) point {
  93. switch action.direction {
  94. case 'R':
  95. return moveRight(leader, tail)
  96. case 'L':
  97. return moveLeft(leader, tail)
  98. case 'U':
  99. return moveUp(leader, tail)
  100. case 'D':
  101. return moveDown(leader, tail)
  102. }
  103. return tail
  104. }
  105. func wayOfTail(moves []move, snake []point) int {
  106. trail := make(map[point]bool)
  107. last := len(snake) - 1
  108. for i := range moves {
  109. for s := 0; s < moves[i].steps; s++ {
  110. for j := range snake {
  111. if j == 0 {
  112. snake[j] = drawTail(nil, snake[j], moves[i])
  113. } else {
  114. snake[j] = drawTail(&snake[j-1], snake[j], moves[i])
  115. }
  116. }
  117. trail[snake[last]] = true
  118. }
  119. }
  120. return len(trail)
  121. }
  122. func main() {
  123. if len(os.Args) < 2 {
  124. log.Fatal("You need to specify a file!")
  125. }
  126. filePath := os.Args[1]
  127. file, err := os.Open(filePath)
  128. if err != nil {
  129. log.Fatalf("Failed to open %s!\n", filePath)
  130. }
  131. moves := readInput(file)
  132. snake1 := make([]point, 2)
  133. fmt.Println("Part1:", wayOfTail(moves, snake1))
  134. snake2 := make([]point, 10)
  135. fmt.Println("Part2:", wayOfTail(moves, snake2))
  136. }