From 199bc88505f7aad3a08e81f78a2854210192de3d Mon Sep 17 00:00:00 2001 From: setop Date: Sun, 3 Dec 2023 01:28:58 +0100 Subject: [PATCH] day 9, brut force all permutations --- d09/part1.py | 38 -------------------------------------- d09/run.py | 22 ++++++++++++++++++++++ 2 files changed, 22 insertions(+), 38 deletions(-) delete mode 100644 d09/part1.py create mode 100644 d09/run.py diff --git a/d09/part1.py b/d09/part1.py deleted file mode 100644 index 59e93b3..0000000 --- a/d09/part1.py +++ /dev/null @@ -1,38 +0,0 @@ -import sys -import networkx as nx - -G = nx.Graph() -D = {} -for l in sys.stdin.readlines(): - l = l[:-1] - I = l.split(' ') - G.add_edge(I[0], I[2], weight=int(I[4])) - #G.add_edge(I[2], I[0], weight=int(I[4])) - D[(I[0], I[2])]=int(I[4]) - D[(I[2], I[0])]=int(I[4]) - - - -def dfs_visit(graph, node, visited): - visited[node] = True - for neighbor in graph.neighbors(node): - if not visited[neighbor]: - dfs_visit(graph, neighbor, visited) - -def dfs(graph): - visited = node: False for node in graph.nodes - for node in graph.nodes: - if not visited[node]: - dfs_visit(graph, node, visited) - - -# Call the DFS function and visit all nodes exactly once -dfs(G) - -tsp = nx.approximation.traveling_salesman_problem - -L = tsp(G)#, cycle=False) -print(L) -S = sum(D[(a,b)] for (a,b) in zip(L[1:],L[2:])) - -print(S) diff --git a/d09/run.py b/d09/run.py new file mode 100644 index 0000000..acbe528 --- /dev/null +++ b/d09/run.py @@ -0,0 +1,22 @@ +import sys +from itertools import permutations as perm + +C = set() # cities +D = dict() # distances + +for line in sys.stdin.readlines(): + c1, _, c2, _, d = line[:-1].split() + d = int(d) + D[(c1, c2)] = d + D[(c2, c1)] = d + C.add(c1) + C.add(c2) + +S = 999999 +L = 0 +for r in perm(C): # brut force all possible routes + l = sum(D[(c1, c2)] + for c1, c2 in zip(r, r[1:])) + S = min(l, S) + L = max(l, L) +print(S, L)