code.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. if p.x-1 >= 0 && board[p.y][p.x-1] != Rock {
  45. destinations = append(destinations, Point{y: p.y, x: p.x - 1, steps: p.steps})
  46. }
  47. if p.x+1 < width && board[p.y][p.x+1] != Rock {
  48. destinations = append(destinations, Point{y: p.y, x: p.x + 1, steps: p.steps})
  49. }
  50. if p.y-1 >= 0 && board[p.y-1][p.x] != Rock {
  51. destinations = append(destinations, Point{y: p.y - 1, x: p.x, steps: p.steps})
  52. }
  53. if p.y+1 < height && board[p.y+1][p.x] != Rock {
  54. destinations = append(destinations, Point{y: p.y + 1, x: p.x, steps: p.steps})
  55. }
  56. return destinations
  57. }
  58. func (p *Point) key() string {
  59. return fmt.Sprintf("%d-%d", p.y, p.x)
  60. }
  61. func calculate(board [][]byte, start Point, maxSteps int) int {
  62. height := len(board)
  63. width := len(board[0])
  64. visited := make(map[string]int)
  65. visited[start.key()] = start.steps
  66. frontier := []Point{start}
  67. for {
  68. if len(frontier) == 0 {
  69. break
  70. }
  71. current := frontier[0]
  72. frontier = frontier[1:]
  73. successors := current.getDestinations(board, height, width, maxSteps)
  74. for i := range successors {
  75. value, ok := visited[successors[i].key()]
  76. if !ok || value < maxSteps && successors[i].steps != value {
  77. visited[successors[i].key()] = successors[i].steps
  78. frontier = append(frontier, successors[i])
  79. }
  80. }
  81. }
  82. count := 0
  83. for _, v := range visited {
  84. if v == maxSteps {
  85. count++
  86. }
  87. }
  88. return count
  89. }
  90. func main() {
  91. if len(os.Args) < 2 {
  92. log.Fatal("You need to specify a file!")
  93. }
  94. filePath := os.Args[1]
  95. file, err := os.Open(filePath)
  96. if err != nil {
  97. log.Fatalf("Failed to open %s!\n", filePath)
  98. }
  99. start, board := readInput(file)
  100. fmt.Println("Part1:", calculate(board, start, 64))
  101. }