import re # Fill Input try: with open("09.txt", 'r', encoding='utf-8') as file: input = file.read() except Exception as e: print(f"An error occurred: {e}") # Result def perm(X, visited = []): if len(visited) == len(X): return [visited] res = [] for a in X: if a not in visited: for i in perm(X, [*visited, a]): res.append(i) return res; path = {} for line in input.split('\n'): if not (re_match := re.search(r"(\w+) to (\w+) = (\d+)", line)): break f = re_match.group(1) t = re_match.group(2) d = int(re_match.group(3)) if f in path: path[f][t] = d else: path[f] = {t:d} if t in path: path[t][f] = d else: path[t] = {f:d} res = [] for p in perm(list(path)): sum = 0; last = None for c in p: if not last: last = c continue sum += path[last][c] last = c res.append(sum) print(min(res), max(res))