code.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 min(results []int) int {
  59. m := results[0]
  60. for i := range results {
  61. if results[i] < m {
  62. m = results[i]
  63. }
  64. }
  65. return m
  66. }
  67. func check(number int, numbers []int) bool {
  68. for i := range numbers {
  69. if number%numbers[i] != 0 {
  70. return false
  71. }
  72. }
  73. return true
  74. }
  75. func calculateResult(results []int) int {
  76. if len(results) == 1 {
  77. return results[0]
  78. }
  79. min := min(results)
  80. current := min
  81. for {
  82. if check(current, results) {
  83. break
  84. }
  85. current += min
  86. }
  87. return current
  88. }
  89. func part(network Network, starts []string, goal string) int {
  90. mod := len(network.moves)
  91. var results []int
  92. for i := range starts {
  93. steps := 0
  94. index := 0
  95. for {
  96. if atGoal(starts[i], goal) {
  97. break
  98. }
  99. turn := network.moves[index]
  100. d := network.paths[starts[i]]
  101. if turn == 'L' {
  102. starts[i] = d.left
  103. } else {
  104. starts[i] = d.right
  105. }
  106. steps++
  107. index = (index + 1) % mod
  108. }
  109. results = append(results, steps)
  110. }
  111. return calculateResult(results)
  112. }
  113. func main() {
  114. if len(os.Args) < 2 {
  115. log.Fatal("You need to specify a file!")
  116. }
  117. filePath := os.Args[1]
  118. file, err := os.Open(filePath)
  119. if err != nil {
  120. log.Fatalf("Failed to open %s!\n", filePath)
  121. }
  122. network := readInput(file)
  123. fmt.Println("Part1:", part(network, []string{"AAA"}, "ZZZ"))
  124. fmt.Println("Part2:", part(network, network.starts, "Z"))
  125. }