code.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 moveRight(leader *point, tail point) point {
  40. if !tailInVicinity(leader, tail) {
  41. if leader != nil {
  42. tail.y = leader.y
  43. tail.x = leader.x - 1
  44. } else {
  45. tail.x++
  46. }
  47. }
  48. return tail
  49. }
  50. func moveLeft(leader *point, tail point) point {
  51. if !tailInVicinity(leader, tail) {
  52. if leader != nil {
  53. tail.y = leader.y
  54. tail.x = leader.x + 1
  55. } else {
  56. tail.x--
  57. }
  58. }
  59. return tail
  60. }
  61. func moveUp(leader *point, tail point) point {
  62. if !tailInVicinity(leader, tail) {
  63. if leader != nil {
  64. tail.x = leader.x
  65. tail.y = leader.y - 1
  66. } else {
  67. tail.y++
  68. }
  69. }
  70. return tail
  71. }
  72. func moveDown(leader *point, tail point) point {
  73. if !tailInVicinity(leader, tail) {
  74. if leader != nil {
  75. tail.x = leader.x
  76. tail.y = leader.y + 1
  77. } else {
  78. tail.y--
  79. }
  80. }
  81. return tail
  82. }
  83. func drawTail(leader *point, tail point, action move) point {
  84. switch action.direction {
  85. case 'R':
  86. return moveRight(leader, tail)
  87. case 'L':
  88. return moveLeft(leader, tail)
  89. case 'U':
  90. return moveUp(leader, tail)
  91. case 'D':
  92. return moveDown(leader, tail)
  93. }
  94. return tail
  95. }
  96. func wayOfTail(moves []move, snake []point) int {
  97. trail := make(map[point]bool)
  98. last := len(snake) - 1
  99. for i := range moves {
  100. for s := 0; s < moves[i].steps; s++ {
  101. for j := range snake {
  102. if j == 0 {
  103. snake[j] = drawTail(nil, snake[j], moves[i])
  104. } else {
  105. snake[j] = drawTail(&snake[j-1], snake[j], moves[i])
  106. }
  107. }
  108. trail[snake[last]] = true
  109. }
  110. }
  111. return len(trail)
  112. }
  113. func main() {
  114. if len(os.Args) < 2 {
  115. log.Fatal("You need to specify a file!")
  116. }
  117. filePath := os.Args[1]
  118. file, err := os.Open(filePath)
  119. if err != nil {
  120. log.Fatalf("Failed to open %s!\n", filePath)
  121. }
  122. moves := readInput(file)
  123. snake1 := make([]point, 2)
  124. fmt.Println("Part1:", wayOfTail(moves, snake1))
  125. snake2 := make([]point, 10)
  126. fmt.Println("Part2:", wayOfTail(moves, snake2))
  127. }