## DEV Community is a community of 785,073 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Discussion on: AoC Day 7: The Sum of Its Parts

Florian Rohrer

Fighting my way up the leaderboard.

My Python Solution

Part 1

``````import networkx as nx
import numpy as np

with open("input.txt") as f:
edges = [(l[5], l[36]) for l in f]

u, v = zip(*edges)
G = nx.DiGraph()

def get_next_fullfilled_node(seen, open_nodes):
for n in sorted(open_nodes):
if all(k in seen for k in G.predecessors(n)):
return n
open_nodes = set(u)-set(v)

seen = set()
while open_nodes:
n = get_next_fullfilled_node(seen, open_nodes)
print(n, end="")
for k in G[n]:
open_nodes -= seen

``````

Part 2

``````class Worker(object):
def __init__(self):
self.slots = np.array([0 for _ in range(10_000)])
def is_free(self, t):
return self.slots[t] == 0
end_t = start_t+ord(c)-ord('A')+61
self.slots[start_t:end_t] = 1
return end_t
def finish_time(self):
return max(i for i, e in enumerate(self.slots) if e == 1)

while True:
for w in workers:
if w.is_free(time):
time += 1

for n in sorted(open_nodes):
ok = True
for k in G.predecessors(n):
ok = False
break
if ok:
return n
return None

workers = [Worker() for _ in range(5)]