code.go 2.9 KB

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