code.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. func moveRight(x int, y int, trail [][]byte, steps int) (int, int, [][]byte) {
  30. x += steps
  31. edge := len(trail[y]) - 1
  32. if x > edge {
  33. trail[y] = append(trail[y], make([]byte, x-edge)...)
  34. }
  35. for i := x - 1; i > x-steps; i-- {
  36. trail[y][i] = '#'
  37. }
  38. return x, y, trail
  39. }
  40. func moveLeft(x int, y int, trail [][]byte, steps int) (int, int, [][]byte) {
  41. x -= steps
  42. for i := x + 1; i < x+steps; i++ {
  43. trail[y][i] = '#'
  44. }
  45. return x, y, trail
  46. }
  47. func moveUp(x int, y int, trail [][]byte, steps int) (int, int, [][]byte) {
  48. width := len(trail[y])
  49. y += steps
  50. edge := len(trail) - 1
  51. if y > edge {
  52. for i := 0; i < y-edge; i++ {
  53. trail = append(trail, make([]byte, width))
  54. }
  55. }
  56. for i := y - steps + 1; i < y; i++ {
  57. trail[i][x] = '#'
  58. }
  59. return x, y, trail
  60. }
  61. func moveDown(x int, y int, trail [][]byte, steps int) (int, int, [][]byte) {
  62. y -= steps
  63. for i := y - 1; i > y-steps; i-- {
  64. trail[i][x] = '#'
  65. }
  66. return x, y, trail
  67. }
  68. func drawTail(x int, y int, action move, trail [][]byte) (int, int, [][]byte) {
  69. switch action.direction {
  70. case 'R':
  71. return moveRight(x, y, trail, action.steps)
  72. case 'L':
  73. return moveLeft(x, y, trail, action.steps)
  74. case 'U':
  75. return moveUp(x, y, trail, action.steps)
  76. case 'D':
  77. return moveDown(x, y, trail, action.steps)
  78. }
  79. return x, y, trail
  80. }
  81. func calculate(trail [][]byte) int {
  82. count := 0
  83. for i := range trail {
  84. for j := range trail[i] {
  85. if trail[i][j] == '#' {
  86. count++
  87. }
  88. }
  89. }
  90. return count
  91. }
  92. func part1(moves []move) int {
  93. trail := [][]byte{make([]byte, 1)}
  94. x := 0
  95. y := 0
  96. for i := range moves {
  97. x, y, trail = drawTail(x, y, moves[i], trail)
  98. }
  99. return calculate(trail)
  100. }
  101. func main() {
  102. if len(os.Args) < 2 {
  103. log.Fatal("You need to specify a file!")
  104. }
  105. filePath := os.Args[1]
  106. file, err := os.Open(filePath)
  107. if err != nil {
  108. log.Fatalf("Failed to open %s!\n", filePath)
  109. }
  110. moves := readInput(file)
  111. fmt.Println("Part1:", part1(moves))
  112. }