package main import ( "bufio" "fmt" "os" "strings" "time" ) type PathState struct { node string visited map[string]bool } type Graph map[string][]string func readFile(fname string) ([]string, error) { file, err := os.Open(fname) if err != nil { return nil, fmt.Errorf("failed to open file: %v", err) } defer file.Close() var puzzle []string scanner := bufio.NewScanner(file) for scanner.Scan() { puzzle = append(puzzle, scanner.Text()) } if err := scanner.Err(); err != nil { return nil, fmt.Errorf("failed to read file: %v", err) } return puzzle, nil } func parseInput(puzzle []string) Graph { graph := make(Graph) for _, line := range puzzle { nodes := strings.Split(line, ":") childs := strings.Fields(nodes[1]) graph[nodes[0]] = childs } return graph } func countPaths( graph map[string][]string, start, end string, memo map[string]int, ) int { if val, ok := memo[start]; ok { return val } if start == end { return 1 } count := 0 for _, neighbor := range graph[start] { count += countPaths(graph, neighbor, end, memo) } memo[start] = count return count } func countPathsWithConstraints( graph map[string][]string, start, end string, requiredNodes map[string]int, visitedMask int, memo map[string]map[int]int, ) int { // Initialise la map de mémoïsation pour ce nœud si nécessaire if memo[start] == nil { memo[start] = make(map[int]int) } if val, ok := memo[start][visitedMask]; ok { return val } // Met à jour le masque pour le nœud actuel (si c'est un nœud obligatoire) newMask := visitedMask if idx, ok := requiredNodes[start]; ok { newMask |= (1 << idx) } // Si on est à la fin, vérifie si tous les nœuds requis sont visités if start == end { if newMask == (1<