code.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. const (
  9. Up = iota
  10. Down
  11. Left
  12. Right
  13. )
  14. type Point struct {
  15. y, x int
  16. direction int
  17. }
  18. func (p *Point) move(maze []string, height int, width int) bool {
  19. switch maze[p.y][p.x] {
  20. case '|':
  21. if p.direction == Up && p.y-1 >= 0 {
  22. p.y--
  23. return true
  24. } else if p.direction == Down && p.y+1 < height {
  25. p.y++
  26. return true
  27. }
  28. case '-':
  29. if p.direction == Right && p.x+1 < width {
  30. p.x++
  31. return true
  32. } else if p.direction == Left && p.x-1 >= 0 {
  33. p.x--
  34. return true
  35. }
  36. case 'L':
  37. if p.direction == Down && p.x+1 < width {
  38. p.x++
  39. p.direction = Right
  40. return true
  41. } else if p.direction == Left && p.y-1 >= 0 {
  42. p.y--
  43. p.direction = Up
  44. return true
  45. }
  46. case 'J':
  47. if p.direction == Down && p.x-1 >= 0 {
  48. p.x--
  49. p.direction = Left
  50. return true
  51. } else if p.direction == Right && p.y-1 >= 0 {
  52. p.y--
  53. p.direction = Up
  54. return true
  55. }
  56. case '7':
  57. if p.direction == Right && p.y+1 < height {
  58. p.y++
  59. p.direction = Down
  60. return true
  61. } else if p.direction == Up && p.x-1 >= 0 {
  62. p.x--
  63. p.direction = Left
  64. return true
  65. }
  66. case 'F':
  67. if p.direction == Up && p.x+1 < width {
  68. p.x++
  69. p.direction = Right
  70. return true
  71. } else if p.direction == Left && p.y+1 < height {
  72. p.y++
  73. p.direction = Down
  74. return true
  75. }
  76. }
  77. return false
  78. }
  79. func part1(maze []string) int {
  80. biggest := 0
  81. height := len(maze)
  82. width := len(maze[0])
  83. for y := range maze {
  84. for x := range maze[y] {
  85. if maze[y][x] == 'F' {
  86. current := Point{y: y, x: x}
  87. start := Point{y: y, x: x}
  88. steps := 0
  89. for {
  90. if !current.move(maze, height, width) {
  91. break
  92. }
  93. steps++
  94. if current.x == start.x && current.y == start.y {
  95. steps /= 2
  96. if steps > biggest {
  97. biggest = steps
  98. }
  99. break
  100. }
  101. }
  102. }
  103. }
  104. }
  105. return biggest
  106. }
  107. func readInput(file *os.File) []string {
  108. scanner := bufio.NewScanner(file)
  109. var maze []string
  110. for scanner.Scan() {
  111. line := scanner.Text()
  112. if line == "" {
  113. break
  114. }
  115. maze = append(maze, line)
  116. }
  117. return maze
  118. }
  119. func main() {
  120. if len(os.Args) < 2 {
  121. log.Fatal("You need to specify a file!")
  122. }
  123. filePath := os.Args[1]
  124. file, err := os.Open(filePath)
  125. if err != nil {
  126. log.Fatalf("Failed to open %s!\n", filePath)
  127. }
  128. maze := readInput(file)
  129. fmt.Println("Part1:", part1(maze))
  130. }