code.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. type Point struct {
  9. y, x int
  10. value byte
  11. }
  12. func key(p Point) string {
  13. return fmt.Sprintf("%d_%d", p.y, p.x)
  14. }
  15. func readInput(file *os.File) [][]byte {
  16. scanner := bufio.NewScanner(file)
  17. var matrix [][]byte
  18. for scanner.Scan() {
  19. line := scanner.Text()
  20. if line == "" {
  21. break
  22. }
  23. matrix = append(matrix, []byte(line))
  24. }
  25. return matrix
  26. }
  27. var directions [][]int = [][]int{
  28. {0, -1}, {1, 0}, {0, 1}, {-1, 0},
  29. }
  30. func getMoves(reindeer Point, matrix [][]byte, xMax, yMax int) []Point {
  31. var moves []Point
  32. for _, direction := range directions {
  33. move := Point{x: reindeer.x + direction[0], y: reindeer.y + direction[1]}
  34. if move.x >= 0 && move.x < xMax &&
  35. move.y >= 0 && move.y < yMax && matrix[move.y][move.x]-reindeer.value == 1 {
  36. move.value = matrix[move.y][move.x]
  37. moves = append(moves, move)
  38. }
  39. }
  40. return moves
  41. }
  42. func hike(reindeer Point, matrix [][]byte, xMax, yMax int, hash func(Point) string) int {
  43. var nines int
  44. visited := make(map[string]bool)
  45. moves := []Point{reindeer}
  46. for len(moves) > 0 {
  47. current := moves[0]
  48. moves = moves[1:]
  49. if matrix[current.y][current.x] == '9' {
  50. nines++
  51. }
  52. newMoves := getMoves(current, matrix, xMax, yMax)
  53. for _, newMove := range newMoves {
  54. if !visited[hash(newMove)] {
  55. moves = append(moves, newMove)
  56. visited[hash(newMove)] = true
  57. }
  58. }
  59. }
  60. return nines
  61. }
  62. func part1(matrix [][]byte) int {
  63. var result int
  64. xMax := len(matrix[0])
  65. yMax := len(matrix)
  66. for y := range matrix {
  67. for x := range matrix[y] {
  68. if matrix[y][x] == '0' {
  69. reindeer := Point{x: x, y: y, value: '0'}
  70. result += hike(reindeer, matrix, xMax, yMax, key)
  71. }
  72. }
  73. }
  74. return result
  75. }
  76. func hike2(reindeer Point, matrix [][]byte, xMax, yMax int) int {
  77. var nines int
  78. moves := []Point{reindeer}
  79. for len(moves) > 0 {
  80. current := moves[0]
  81. moves = moves[1:]
  82. if matrix[current.y][current.x] == '9' {
  83. nines++
  84. }
  85. newMoves := getMoves(current, matrix, xMax, yMax)
  86. for _, newMove := range newMoves {
  87. moves = append(moves, newMove)
  88. }
  89. }
  90. return nines
  91. }
  92. func part2(matrix [][]byte) int {
  93. var result int
  94. xMax := len(matrix[0])
  95. yMax := len(matrix)
  96. for y := range matrix {
  97. for x := range matrix[y] {
  98. if matrix[y][x] == '0' {
  99. reindeer := Point{x: x, y: y, value: '0'}
  100. result += hike2(reindeer, matrix, xMax, yMax)
  101. }
  102. }
  103. }
  104. return result
  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. matrix := readInput(file)
  116. fmt.Println("Part1:", part1(matrix))
  117. fmt.Println("Part2:", part2(matrix))
  118. }