code.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. const (
  9. Diff = 48
  10. )
  11. func readInput(file *os.File) [][]int {
  12. scanner := bufio.NewScanner(file)
  13. var board [][]int
  14. for scanner.Scan() {
  15. line := scanner.Text()
  16. if line == "" {
  17. break
  18. }
  19. var row []int
  20. for i := range line {
  21. row = append(row, int(line[i]-Diff))
  22. }
  23. board = append(board, row)
  24. }
  25. return board
  26. }
  27. const (
  28. North = iota
  29. East
  30. South
  31. West
  32. )
  33. type Point struct {
  34. y, x int
  35. }
  36. type Destination struct {
  37. pos Point
  38. cost int
  39. moves int
  40. direction int
  41. }
  42. func getNorth(board [][]int, height int, width int, lava Destination, minMoves int, maxMoves int) []Destination {
  43. var destinations []Destination
  44. moves := 0
  45. if lava.direction == North {
  46. moves = lava.moves
  47. }
  48. end := lava.pos.y - maxMoves
  49. if end < 0 {
  50. end = 0
  51. }
  52. cost := lava.cost
  53. for y := lava.pos.y - 1; y >= end; y-- {
  54. cost += board[y][lava.pos.x]
  55. moves++
  56. if moves > maxMoves {
  57. break
  58. }
  59. if moves > minMoves {
  60. destinations = append(destinations, Destination{pos: Point{y: y, x: lava.pos.x}, moves: moves, cost: cost, direction: North})
  61. }
  62. }
  63. return destinations
  64. }
  65. func getEast(board [][]int, height int, width int, lava Destination, minMoves int, maxMoves int) []Destination {
  66. var destinations []Destination
  67. moves := 0
  68. if lava.direction == East {
  69. moves = lava.moves
  70. }
  71. end := lava.pos.x + maxMoves
  72. if end >= width {
  73. end = width - 1
  74. }
  75. cost := lava.cost
  76. for x := lava.pos.x + 1; x <= end; x++ {
  77. cost += board[lava.pos.y][x]
  78. moves++
  79. if moves > maxMoves {
  80. break
  81. }
  82. if moves > minMoves {
  83. destinations = append(destinations, Destination{pos: Point{y: lava.pos.y, x: x}, moves: moves, cost: cost, direction: East})
  84. }
  85. }
  86. return destinations
  87. }
  88. func getSouth(board [][]int, height int, width int, lava Destination, minMoves int, maxMoves int) []Destination {
  89. var destinations []Destination
  90. moves := 0
  91. if lava.direction == South {
  92. moves = lava.moves
  93. }
  94. end := lava.pos.y + maxMoves
  95. if end >= height {
  96. end = height - 1
  97. }
  98. cost := lava.cost
  99. for y := lava.pos.y + 1; y <= end; y++ {
  100. cost += board[y][lava.pos.x]
  101. moves++
  102. if moves > maxMoves {
  103. break
  104. }
  105. if moves > minMoves {
  106. destinations = append(destinations, Destination{pos: Point{y: y, x: lava.pos.x}, moves: moves, cost: cost, direction: South})
  107. }
  108. }
  109. return destinations
  110. }
  111. func getWest(board [][]int, height int, width int, lava Destination, minMoves int, maxMoves int) []Destination {
  112. var destinations []Destination
  113. moves := 0
  114. if lava.direction == West {
  115. moves = lava.moves
  116. }
  117. end := lava.pos.x - maxMoves
  118. if end < 0 {
  119. end = 0
  120. }
  121. cost := lava.cost
  122. for x := lava.pos.x - 1; x >= end; x-- {
  123. cost += board[lava.pos.y][x]
  124. moves++
  125. if moves > maxMoves {
  126. break
  127. }
  128. if moves > minMoves {
  129. destinations = append(destinations, Destination{pos: Point{y: lava.pos.y, x: x}, moves: moves, cost: cost, direction: West})
  130. }
  131. }
  132. return destinations
  133. }
  134. func getDirections(direction int) []int {
  135. switch direction {
  136. case North:
  137. return []int{East, North, West}
  138. case East:
  139. return []int{East, South, North}
  140. case South:
  141. return []int{East, South, West}
  142. case West:
  143. return []int{South, West, North}
  144. }
  145. return []int{}
  146. }
  147. func getDestinations(board [][]int, height int, width int, lava Destination, minMoves int, maxMoves int) []Destination {
  148. var destinations []Destination
  149. directions := getDirections(lava.direction)
  150. for i := range directions {
  151. switch directions[i] {
  152. case North:
  153. destinations = append(destinations, getNorth(board, height, width, lava, minMoves, maxMoves)...)
  154. case East:
  155. destinations = append(destinations, getEast(board, height, width, lava, minMoves, maxMoves)...)
  156. case South:
  157. destinations = append(destinations, getSouth(board, height, width, lava, minMoves, maxMoves)...)
  158. case West:
  159. destinations = append(destinations, getWest(board, height, width, lava, minMoves, maxMoves)...)
  160. }
  161. }
  162. return destinations
  163. }
  164. type Visited struct {
  165. pos Point
  166. direction int
  167. }
  168. func calculate(board [][]int, minMoves int, maxMoves int) int {
  169. min := 1000000
  170. height := len(board)
  171. width := len(board[0])
  172. goal := Point{y: height - 1, x: width - 1}
  173. explored := make(map[Visited]int)
  174. lava := Destination{pos: Point{x: 0, y: 0}, moves: 0, direction: East}
  175. frontier := []Destination{lava}
  176. for {
  177. if len(frontier) == 0 {
  178. break
  179. }
  180. current := frontier[0]
  181. frontier = frontier[1:]
  182. if current.pos == goal {
  183. if min > current.cost {
  184. min = current.cost
  185. }
  186. }
  187. successors := getDestinations(board, height, width, current, minMoves, maxMoves)
  188. for i := range successors {
  189. v := Visited{pos: successors[i].pos, direction: successors[i].direction}
  190. value, ok := explored[v]
  191. if !ok || value > successors[i].cost {
  192. explored[v] = successors[i].cost
  193. frontier = append(frontier, successors[i])
  194. }
  195. }
  196. }
  197. return min
  198. }
  199. func main() {
  200. if len(os.Args) < 2 {
  201. log.Fatal("You need to specify a file!")
  202. }
  203. filePath := os.Args[1]
  204. file, err := os.Open(filePath)
  205. if err != nil {
  206. log.Fatalf("Failed to open %s!\n", filePath)
  207. }
  208. board := readInput(file)
  209. fmt.Println("Part1:", calculate(board, 0, 3))
  210. fmt.Println("Part2:", calculate(board, 3, 10))
  211. }