-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6.py
60 lines (49 loc) · 952 Bytes
/
day6.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
inputs = """
COM)B
B)C
C)D
D)E
E)F
B)G
G)H
D)I
E)J
J)K
K)L
"""
file1 = open("day6_input.txt","r")
inputs = file1.readlines()
file1.close()
s2 = [x.replace('\n','').split(')') for x in inputs if x]
# Make dict for graph
orbit_graph = {}
for x in s2:
try:
orbit_graph[x[1]] = x[0]
except:
print(x)
# Traverse all
orbits = 0
for k,v in orbit_graph.items():
current_key = k
while current_key != 'COM':
try:
next_key = orbit_graph[current_key]
orbits +=1
current_key = next_key
except:
print('EROR', current_key)
break
print(orbits)
# Part two
import networkx as nx
# Make dict for graph
G=nx.Graph()
# Add nodes
for k,v in orbit_graph.items():
G.add_node(k)
# Add edges
for x in s2:
G.add_edge(x[1],x[0])
# shortest path
nx.shortest_path_length(G, source=orbit_graph.get('YOU'), target= orbit_graph.get('SAN'), weight=None)