code.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. type Point struct {
  9. y, x int
  10. steps int
  11. }
  12. func readInput(file *os.File) (Point, [][]byte) {
  13. scanner := bufio.NewScanner(file)
  14. var board [][]byte
  15. var start Point
  16. var index int
  17. for scanner.Scan() {
  18. line := scanner.Text()
  19. if line == "" {
  20. break
  21. }
  22. var row []byte
  23. for i := range line {
  24. if line[i] == 'S' {
  25. start.x = i
  26. start.y = index
  27. }
  28. row = append(row, line[i])
  29. }
  30. board = append(board, row)
  31. index++
  32. }
  33. return start, board
  34. }
  35. const (
  36. Rock = '#'
  37. )
  38. func (p *Point) getDestinations(board [][]byte, height int, width int, maxSteps int) []Point {
  39. var destinations []Point
  40. p.steps++
  41. if p.steps > maxSteps {
  42. return destinations
  43. }
  44. x := (p.x - 1) % width
  45. if x < 0 {
  46. x = width + x
  47. }
  48. y := p.y % height
  49. if y < 0 {
  50. y = height + y
  51. }
  52. if board[y][x] != Rock {
  53. destinations = append(destinations, Point{y: p.y, x: p.x - 1, steps: p.steps})
  54. }
  55. x = (x + 2) % width
  56. if board[y][x] != Rock {
  57. destinations = append(destinations, Point{y: p.y, x: p.x + 1, steps: p.steps})
  58. }
  59. x = (x - 1) % width
  60. if x < 0 {
  61. x = width + x
  62. }
  63. y = (y - 1) % height
  64. if y < 0 {
  65. y = height + y
  66. }
  67. if board[y][x] != Rock {
  68. destinations = append(destinations, Point{y: p.y - 1, x: p.x, steps: p.steps})
  69. }
  70. y = (y + 2) % height
  71. if board[y][x] != Rock {
  72. destinations = append(destinations, Point{y: p.y + 1, x: p.x, steps: p.steps})
  73. }
  74. return destinations
  75. }
  76. func (p *Point) key() string {
  77. return fmt.Sprintf("%d_%d", p.y, p.x)
  78. }
  79. func calculate(board [][]byte, start Point, maxSteps int) int {
  80. height := len(board)
  81. width := len(board[0])
  82. visited := make(map[string]int)
  83. visited[start.key()] = start.steps
  84. frontier := []Point{start}
  85. for {
  86. if len(frontier) == 0 {
  87. break
  88. }
  89. current := frontier[0]
  90. frontier = frontier[1:]
  91. successors := current.getDestinations(board, height, width, maxSteps)
  92. for i := range successors {
  93. value, ok := visited[successors[i].key()]
  94. if !ok || value < maxSteps && successors[i].steps != value {
  95. visited[successors[i].key()] = successors[i].steps
  96. frontier = append(frontier, successors[i])
  97. }
  98. }
  99. }
  100. count := 0
  101. for _, v := range visited {
  102. if v == maxSteps {
  103. count++
  104. }
  105. }
  106. return count
  107. }
  108. func main() {
  109. if len(os.Args) < 2 {
  110. log.Fatal("You need to specify a file!")
  111. }
  112. filePath := os.Args[1]
  113. file, err := os.Open(filePath)
  114. if err != nil {
  115. log.Fatalf("Failed to open %s!\n", filePath)
  116. }
  117. start, board := readInput(file)
  118. fmt.Println("Part1:", calculate(board, start, 64))
  119. }