From 69f4c3c8e70c2b402950b5d820e2571a10a4a872 Mon Sep 17 00:00:00 2001 From: setop Date: Fri, 1 Dec 2023 18:13:17 +0100 Subject: [PATCH] skip day 7, needs an AST ; done day 8 ; draft day 9 --- d08/run.py | 11 +++++++++++ d09/part1.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 d08/run.py create mode 100644 d09/part1.py diff --git a/d08/run.py b/d08/run.py new file mode 100644 index 0000000..79f78c7 --- /dev/null +++ b/d08/run.py @@ -0,0 +1,11 @@ +import sys + +R = T = N = 0 +for l in sys.stdin.readlines(): + l = l[:-1] + s = eval(l) + T += len(l) + R += len(s) + N += sum((2 if c in ['"', '\\'] else 1 for c in l), 2) + #print(len(l),l,len(s), s, nl) +print(T, R, T-R, N-T) \ No newline at end of file diff --git a/d09/part1.py b/d09/part1.py new file mode 100644 index 0000000..59e93b3 --- /dev/null +++ b/d09/part1.py @@ -0,0 +1,38 @@ +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)