# Advent of Code Day 6¶

In :
testinput = """COM)B
B)C
C)D
D)E
E)F
B)G
G)H
D)I
E)J
J)K
K)L"""

In :
from collections import defaultdict
def process(input_text):
orbits = input_text.split("\n")
connections = [orbit.split(")") for orbit in orbits]
graph = defaultdict(lambda: set())
roots = set()
for connection in connections:
for connection in connections:
if connection in roots:
roots.remove(connection)

return roots, graph

In :
process(testinput)

Out:
({'COM'},
defaultdict(<function __main__.process.<locals>.<lambda>()>,
{'COM': {'B'},
'B': {'C', 'G'},
'C': {'D'},
'D': {'E', 'I'},
'E': {'F', 'J'},
'G': {'H'},
'J': {'K'},
'K': {'L'}}))
In :
total = 0
visited = set()

def count_paths(roots, graph, depth = 0):
global total
for root in roots:
if root in visited:
continue

total += depth

if root in graph:
count_paths(graph[root], graph, depth + 1)

In :
count_paths(*process(testinput))

In :
total

Out:
42
In :
total = 0
with open("AoC/input6.txt") as inputfile:

In :
total

Out:
251208
In :
roots, graph, bigraph = None, None, defaultdict(lambda: set())
with open("AoC/input6.txt") as inputfile:
for root in graph:
bigraph[root].update(graph[root])
for pt in graph[root]:

In :
[(x, y) for (x, y) in graph.items() if "SAN" in y or "YOU" in y]

Out:
[('Z48', {'YOU'}), ('87T', {'SAN'})]
In :
distances = dict()
def bfs(start, graph, dist=0):
if start in distances:
return

distances[start] = dist
for node in graph.get(start, set()):
bfs(node, graph, dist + 1)

In :
bfs('Z48', bigraph)

In :
distances['Z48']

Out:
0
In :
distances['87T']

Out:
397
In [ ]: