code.go 2.5 KB

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