package main import ( "bufio" "fmt" "log" "os" "regexp" "strings" ) type valve struct { name string rate int open bool connections []string } func readInput(file *os.File) ([]string, map[string]valve) { scanner := bufio.NewScanner(file) valves := make(map[string]valve) var vertices []string for scanner.Scan() { line := scanner.Text() if line == "" { continue } var current valve if strings.Contains(line, "valves") { n, err := fmt.Sscanf(line, "Valve %s has flow rate=%d", ¤t.name, ¤t.rate) if n != 2 || err != nil { log.Fatal("Can't parse (valves):", line, err) } re := regexp.MustCompile(`valves .*`) parts := re.FindString(line) parts = strings.TrimLeft(parts, "valves ") current.connections = strings.Split(parts, ", ") } else { var connection string n, err := fmt.Sscanf(line, "Valve %s has flow rate=%d; tunnel leads to valve %s", ¤t.name, ¤t.rate, &connection) if n != 3 || err != nil { log.Fatal("Can't parse:", line, err) } current.connections = append(current.connections, connection) } vertices = append(vertices, current.name) valves[current.name] = current } return vertices, valves } type path struct { from string to string cost int } func buildGraph(valves map[string]valve) []path { var graph []path for key, value := range valves { for i := range value.connections { graph = append(graph, path{key, value.connections[i], 1}) } } return graph } func main() { if len(os.Args) < 2 { log.Fatal("You need to specify a file!") } filePath := os.Args[1] file, err := os.Open(filePath) if err != nil { log.Fatalf("Failed to open %s!\n", filePath) } vertices, valves := readInput(file) graph := buildGraph(valves) fmt.Println(vertices, graph) }