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 part1(moves []move) int {
  97. parts := make([]point, 2)
  98. trail := make(map[point]bool)
  99. last := len(parts) - 1
  100. for i := range moves {
  101. for s := 0; s < moves[i].steps; s++ {
  102. for j := range parts {
  103. if j == 0 {
  104. parts[j] = drawTail(nil, parts[j], moves[i])
  105. } else {
  106. parts[j] = drawTail(&parts[j-1], parts[j], moves[i])
  107. }
  108. if j == last {
  109. trail[parts[j]] = true
  110. }
  111. }
  112. fmt.Println(parts)
  113. }
  114. }
  115. return len(trail)
  116. }
  117. func main() {
  118. if len(os.Args) < 2 {
  119. log.Fatal("You need to specify a file!")
  120. }
  121. filePath := os.Args[1]
  122. file, err := os.Open(filePath)
  123. if err != nil {
  124. log.Fatalf("Failed to open %s!\n", filePath)
  125. }
  126. moves := readInput(file)
  127. fmt.Println("Part1:", part1(moves))
  128. }