[Python/2019] Restructure solutions

This commit is contained in:
Felix Bargfeldt 2023-10-29 19:47:15 +01:00
parent 40e767096e
commit fb42493fff
Signed by: Defelo
GPG key ID: 2A05272471204DD3
63 changed files with 1393 additions and 1739 deletions

39
Python/2019/06.py Normal file
View file

@ -0,0 +1,39 @@
from lib import *
input = read_input(2019, 6)
graph = {}
for line in input.splitlines():
a, b = line.split(")")
graph.setdefault(a, set()).add(b)
out = 0
Q = [("COM", 0)]
while Q:
node, count = Q.pop(0)
out += count
for child in graph.get(node, set()):
Q.append((child, count + 1))
print(out)
graph = {}
for line in input.splitlines():
a, b = line.split(")")
graph.setdefault(a, set()).add(b)
graph.setdefault(b, set()).add(a)
Q = [("YOU", 0)]
visited = set()
while Q:
node, dist = Q.pop(0)
if node in visited:
continue
visited.add(node)
if node == "SAN":
print(dist - 2)
for child in graph.get(node, set()):
Q.append((child, dist + 1))