2018/Day07/Day7A.py

33 lines
669 B
Python
Raw Permalink Normal View History

2018-12-07 09:55:39 +00:00
import fileinput
from collections import defaultdict
def build_graph():
graph = defaultdict(list)
for line in fileinput.input():
a, b = line[5], line[36]
graph[b].append(a)
graph[a]
return graph
def main():
graph = build_graph()
order = []
to_consider = sorted(graph.keys())
while to_consider:
for i, node in enumerate(to_consider):
parents = graph[node]
if any(parent in to_consider for parent in parents):
continue
order.append(node)
del to_consider[i]
break
print("".join(order))
if __name__ == "__main__":
main()