code.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "regexp"
  8. "strings"
  9. )
  10. type valve struct {
  11. name string
  12. rate int
  13. open bool
  14. connections []string
  15. }
  16. type vertex struct {
  17. name string
  18. cost int
  19. visited bool
  20. }
  21. const maxValue int = 100000
  22. func readInput(file *os.File) ([]vertex, map[string]valve) {
  23. scanner := bufio.NewScanner(file)
  24. valves := make(map[string]valve)
  25. var vertices []vertex
  26. for scanner.Scan() {
  27. line := scanner.Text()
  28. if line == "" {
  29. continue
  30. }
  31. var current valve
  32. if strings.Contains(line, "valves") {
  33. n, err := fmt.Sscanf(line, "Valve %s has flow rate=%d", &current.name, &current.rate)
  34. if n != 2 || err != nil {
  35. log.Fatal("Can't parse (valves):", line, err)
  36. }
  37. re := regexp.MustCompile(`valves .*`)
  38. parts := re.FindString(line)
  39. parts = strings.TrimLeft(parts, "valves ")
  40. current.connections = strings.Split(parts, ", ")
  41. } else {
  42. var connection string
  43. n, err := fmt.Sscanf(line, "Valve %s has flow rate=%d; tunnel leads to valve %s", &current.name, &current.rate, &connection)
  44. if n != 3 || err != nil {
  45. log.Fatal("Can't parse:", line, err)
  46. }
  47. current.connections = append(current.connections, connection)
  48. }
  49. vertices = append(vertices, vertex{current.name, maxValue, false})
  50. valves[current.name] = current
  51. }
  52. return vertices, valves
  53. }
  54. type path struct {
  55. from string
  56. to string
  57. cost int
  58. }
  59. func buildGraph(valves map[string]valve) []path {
  60. var graph []path
  61. for key, value := range valves {
  62. for i := range value.connections {
  63. graph = append(graph, path{key, value.connections[i], 1})
  64. }
  65. }
  66. return graph
  67. }
  68. func setCost(name string, cost int, vertices []vertex) {
  69. for i := range vertices {
  70. if vertices[i].name == name {
  71. vertices[i].cost = cost
  72. break
  73. }
  74. }
  75. }
  76. func getCost(name string, vertices []vertex) int {
  77. for i := range vertices {
  78. if vertices[i].name == name {
  79. return vertices[i].cost
  80. }
  81. }
  82. return 0
  83. }
  84. func getNext(vertices []vertex, valves map[string]valve) *vertex {
  85. min := maxValue
  86. var current *vertex
  87. for i := range vertices {
  88. if vertices[i].visited {
  89. continue
  90. }
  91. if vertices[i].cost <= min {
  92. min = vertices[i].cost
  93. current = &vertices[i]
  94. }
  95. }
  96. return current
  97. }
  98. func traverse(vertices []vertex, graph []path, valves map[string]valve) {
  99. current := &vertices[0]
  100. current.cost = 0
  101. for {
  102. for j := range graph {
  103. if graph[j].from != current.name {
  104. continue
  105. }
  106. var tentativeCost int
  107. if current.cost == maxValue {
  108. tentativeCost = maxValue
  109. } else {
  110. tentativeCost = current.cost + graph[j].cost
  111. }
  112. if tentativeCost < getCost(graph[j].to, vertices) {
  113. setCost(graph[j].to, tentativeCost, vertices)
  114. }
  115. }
  116. val, _ := valves[current.name]
  117. val.open = true
  118. valves[current.name] = val
  119. current.visited = true
  120. current = getNext(vertices, valves)
  121. if current == nil {
  122. break
  123. }
  124. }
  125. }
  126. func main() {
  127. if len(os.Args) < 2 {
  128. log.Fatal("You need to specify a file!")
  129. }
  130. filePath := os.Args[1]
  131. file, err := os.Open(filePath)
  132. if err != nil {
  133. log.Fatalf("Failed to open %s!\n", filePath)
  134. }
  135. vertices, valves := readInput(file)
  136. graph := buildGraph(valves)
  137. traverse(vertices, graph, valves)
  138. fmt.Println(vertices, valves)
  139. }