code.go 2.6 KB

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