code.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strings"
  8. )
  9. type Directions struct {
  10. left string
  11. right string
  12. }
  13. type Network struct {
  14. moves string
  15. paths map[string]Directions
  16. starts []string
  17. }
  18. func readInput(file *os.File) Network {
  19. scanner := bufio.NewScanner(file)
  20. var network Network
  21. readMoves := false
  22. for scanner.Scan() {
  23. line := scanner.Text()
  24. if line == "" {
  25. if !readMoves {
  26. readMoves = true
  27. continue
  28. }
  29. break
  30. }
  31. if !readMoves {
  32. network.moves = line
  33. network.paths = make(map[string]Directions)
  34. } else {
  35. fromParts := strings.Split(line, " = ")
  36. if len(fromParts) != 2 {
  37. log.Fatalf("Wrong number of fromParts: %s", line)
  38. }
  39. from := fromParts[0]
  40. parts := strings.Split(fromParts[1], ", ")
  41. if len(parts) != 2 {
  42. log.Fatalf("Wrong number of parts: %s", fromParts[1])
  43. }
  44. var directions Directions
  45. directions.left = strings.TrimLeft(parts[0], "(")
  46. directions.right = strings.TrimRight(parts[1], ")")
  47. network.paths[from] = directions
  48. if strings.HasSuffix(from, "A") {
  49. network.starts = append(network.starts, from)
  50. }
  51. }
  52. }
  53. return network
  54. }
  55. func atGoal(starts string, goal string) bool {
  56. return strings.HasSuffix(starts, goal)
  57. }
  58. func max(a, b int) int {
  59. if a > b {
  60. return a
  61. }
  62. return b
  63. }
  64. func min(a, b int) int {
  65. if a < b {
  66. return a
  67. }
  68. return b
  69. }
  70. func gcd(a, b int) int {
  71. if a == 0 {
  72. return b
  73. }
  74. if b == 0 {
  75. return a
  76. }
  77. l := min(a, b)
  78. h := max(a, b)
  79. return gcd(l, h%l)
  80. }
  81. func lcm(a, b int) int {
  82. return (a * b) / gcd(a, b)
  83. }
  84. func part(network Network, starts []string, goal string) int {
  85. mod := len(network.moves)
  86. result := 1
  87. for i := range starts {
  88. steps := 0
  89. index := 0
  90. for {
  91. if atGoal(starts[i], goal) {
  92. break
  93. }
  94. turn := network.moves[index]
  95. d := network.paths[starts[i]]
  96. if turn == 'L' {
  97. starts[i] = d.left
  98. } else {
  99. starts[i] = d.right
  100. }
  101. steps++
  102. index = (index + 1) % mod
  103. }
  104. result = lcm(result, steps)
  105. }
  106. return result
  107. }
  108. func main() {
  109. if len(os.Args) < 2 {
  110. log.Fatal("You need to specify a file!")
  111. }
  112. filePath := os.Args[1]
  113. file, err := os.Open(filePath)
  114. if err != nil {
  115. log.Fatalf("Failed to open %s!\n", filePath)
  116. }
  117. network := readInput(file)
  118. fmt.Println("Part1:", part(network, []string{"AAA"}, "ZZZ"))
  119. fmt.Println("Part2:", part(network, network.starts, "Z"))
  120. }