code.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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, check func(int) bool) bool {
  48. if guard.x < 0 || guard.x > xMax || guard.y < 0 || guard.y > yMax {
  49. return true
  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. if check != nil {
  58. if check(visited[guard.key()]) {
  59. return false
  60. }
  61. }
  62. guard.x += directions[guard.direction][0]
  63. guard.y += directions[guard.direction][1]
  64. }
  65. return walk(guard, matrix, xMax, yMax, visited, check)
  66. }
  67. func part1(guard *Point, matrix [][]byte) map[string]int {
  68. xMax := len(matrix[0]) - 1
  69. yMax := len(matrix) - 1
  70. visited := make(map[string]int)
  71. newGuard := Point{x: guard.x, y: guard.y}
  72. walk(&newGuard, matrix, xMax, yMax, visited, nil)
  73. return visited
  74. }
  75. func part2(guard *Point, matrix [][]byte, expected map[string]int) int {
  76. xMax := len(matrix[0]) - 1
  77. yMax := len(matrix) - 1
  78. var count int
  79. for key, _ := range expected {
  80. var x, y int
  81. fmt.Sscanf(key, "%d_%d", &x, &y)
  82. matrix[y][x] = '#'
  83. visited := make(map[string]int)
  84. newGuard := Point{x: guard.x, y: guard.y}
  85. if !walk(&newGuard, matrix, xMax, yMax, visited, func(a int) bool { return a > 4 }) {
  86. count++
  87. }
  88. matrix[y][x] = '.'
  89. }
  90. return count
  91. }
  92. func main() {
  93. if len(os.Args) < 2 {
  94. log.Fatal("You need to specify a file!")
  95. }
  96. filePath := os.Args[1]
  97. file, err := os.Open(filePath)
  98. if err != nil {
  99. log.Fatalf("Failed to open %s!\n", filePath)
  100. }
  101. guard, matrix := readInput(file)
  102. visited := part1(guard, matrix)
  103. fmt.Println("Part1:", len(visited))
  104. fmt.Println("Part2:", part2(guard, matrix, visited))
  105. }