code.go 5.1 KB

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