code.go 2.8 KB

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