58 lines
1.2 KiB
Python
58 lines
1.2 KiB
Python
from lib import get_data, str_to_ints
|
|
from collections import defaultdict
|
|
|
|
|
|
def part_1(data):
|
|
orbits = defaultdict(list)
|
|
for line in data.splitlines():
|
|
a, b = line.split(")")
|
|
orbits[a].append(b)
|
|
|
|
count = 0
|
|
for key in orbits.keys():
|
|
seen = set()
|
|
around = list(orbits[key])
|
|
while around:
|
|
a = around.pop()
|
|
count += 1
|
|
if a in orbits:
|
|
for o in orbits[a]:
|
|
if o not in seen:
|
|
around.append(o)
|
|
seen.add(a)
|
|
print(count)
|
|
|
|
|
|
def part_2(data):
|
|
g = defaultdict(list)
|
|
for line in data.splitlines():
|
|
a, b = line.split(")")
|
|
g[a].append(b)
|
|
g[b].append(a)
|
|
|
|
seen = set()
|
|
nodes = ["YOU"]
|
|
steps = 0
|
|
while True:
|
|
new_nodes = []
|
|
for node in nodes:
|
|
seen.add(node)
|
|
for nb in g[node]:
|
|
if nb not in seen:
|
|
new_nodes.append(nb)
|
|
if nb == "SAN":
|
|
print(steps - 1)
|
|
return
|
|
nodes = new_nodes
|
|
steps += 1
|
|
|
|
|
|
def main():
|
|
data = get_data(__file__)
|
|
part_1(data)
|
|
part_2(data)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|