code.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 headTail struct {
  30. headX, headY int
  31. tailX, tailY int
  32. }
  33. func tailInVicinity(tracker headTail) bool {
  34. return tracker.tailX >= tracker.headX-1 && tracker.tailX <= tracker.headX+1 && tracker.tailY >= tracker.headY-1 && tracker.tailY <= tracker.headY+1
  35. }
  36. func moveRight(tracker headTail, trail [][]byte, steps int) (headTail, [][]byte) {
  37. destination := tracker.headX + steps
  38. edge := len(trail[tracker.headY]) - 1
  39. if destination > edge {
  40. trail[tracker.headY] = append(trail[tracker.headY], make([]byte, destination-edge)...)
  41. }
  42. for i := tracker.headX + 1; i <= destination; i++ {
  43. tracker.headX = i
  44. if !tailInVicinity(tracker) {
  45. tracker.tailY = tracker.headY
  46. tracker.tailX = i - 1
  47. trail[tracker.tailY][tracker.tailX] = '#'
  48. }
  49. }
  50. return tracker, trail
  51. }
  52. func moveLeft(tracker headTail, trail [][]byte, steps int) (headTail, [][]byte) {
  53. destination := tracker.headX - steps
  54. if destination < 0 {
  55. add := 0 - destination
  56. destination = 0
  57. tracker.headX = add
  58. trail[tracker.headY] = append(make([]byte, add), trail[tracker.headY]...)
  59. }
  60. for i := tracker.headX - 1; i >= destination; i-- {
  61. tracker.headX = i
  62. if !tailInVicinity(tracker) {
  63. edge := len(trail[tracker.headY]) - 1
  64. if edge < i+1 {
  65. trail[tracker.headY] = append(trail[tracker.headY], make([]byte, i-edge+1)...)
  66. }
  67. tracker.tailY = tracker.headY
  68. tracker.tailX = i + 1
  69. trail[tracker.tailY][tracker.tailX] = '#'
  70. }
  71. }
  72. return tracker, trail
  73. }
  74. func moveUp(tracker headTail, trail [][]byte, steps int) (headTail, [][]byte) {
  75. destination := tracker.headY + steps
  76. edge := len(trail) - 1
  77. if destination > edge {
  78. width := len(trail[tracker.headY])
  79. for i := 0; i < destination-edge; i++ {
  80. trail = append(trail, make([]byte, width))
  81. }
  82. }
  83. for i := tracker.headY + 1; i <= destination; i++ {
  84. tracker.headY = i
  85. edge := len(trail[i]) - 1
  86. if tracker.headX > edge {
  87. trail[i] = append(trail[i], make([]byte, tracker.headX-edge)...)
  88. }
  89. if !tailInVicinity(tracker) {
  90. tracker.tailX = tracker.headX
  91. tracker.tailY = i - 1
  92. trail[tracker.tailY][tracker.tailX] = '#'
  93. }
  94. }
  95. return tracker, trail
  96. }
  97. func moveDown(tracker headTail, trail [][]byte, steps int) (headTail, [][]byte) {
  98. destination := tracker.headY - steps
  99. if destination < 0 {
  100. add := 0 - destination
  101. add++
  102. destination = 0
  103. tracker.headY = add
  104. width := len(trail[tracker.headY])
  105. var toPrepend [][]byte
  106. for i := 0; i < add; i++ {
  107. toPrepend = append(toPrepend, make([]byte, width))
  108. }
  109. trail = append(toPrepend, trail...)
  110. }
  111. for i := tracker.headY - 1; i >= destination; i-- {
  112. tracker.headY = i
  113. if !tailInVicinity(tracker) {
  114. tracker.tailX = tracker.headX
  115. tracker.tailY = i + 1
  116. edge := len(trail[tracker.tailY]) - 1
  117. if edge < tracker.tailX {
  118. trail[tracker.tailY] = append(trail[tracker.tailY], make([]byte, tracker.tailX-edge)...)
  119. }
  120. trail[tracker.tailY][tracker.tailX] = '#'
  121. }
  122. }
  123. return tracker, trail
  124. }
  125. func drawTail(tracker headTail, action move, trail [][]byte) (headTail, [][]byte) {
  126. switch action.direction {
  127. case 'R':
  128. return moveRight(tracker, trail, action.steps)
  129. case 'L':
  130. return moveLeft(tracker, trail, action.steps)
  131. case 'U':
  132. return moveUp(tracker, trail, action.steps)
  133. case 'D':
  134. return moveDown(tracker, trail, action.steps)
  135. }
  136. return tracker, trail
  137. }
  138. func calculate(trail [][]byte) int {
  139. count := 0
  140. for i := range trail {
  141. for j := range trail[i] {
  142. if trail[i][j] == '#' {
  143. count++
  144. }
  145. }
  146. }
  147. return count
  148. }
  149. func part1(moves []move) int {
  150. trail := [][]byte{[]byte{'#'}}
  151. var tracker headTail
  152. for i := range moves {
  153. tracker, trail = drawTail(tracker, moves[i], trail)
  154. }
  155. return calculate(trail)
  156. }
  157. func main() {
  158. if len(os.Args) < 2 {
  159. log.Fatal("You need to specify a file!")
  160. }
  161. filePath := os.Args[1]
  162. file, err := os.Open(filePath)
  163. if err != nil {
  164. log.Fatalf("Failed to open %s!\n", filePath)
  165. }
  166. moves := readInput(file)
  167. fmt.Println("Part1:", part1(moves))
  168. }