### re: AoC Day 7: The Sum of Its Parts VIEW POST

Solved super late but:

shared:

``````def clean_input(data):
relationships = defaultdict(set)
for line in data:
_, parent, child = re.findall(r'[A-Z]', line)
if parent not in relationships:
relationships[parent] = set()
return relationships

with open ('input.txt', 'r') as f:
relationships = clean_input(f)
``````

p1

``````def find_order(relationships):
done = []
while len(done) < len(relationships):
for node in relationships:
if node not in done and relationships[node].issubset(done):
return done

print(''.join(find_order(relationships)))
``````

p2

``````
def letter_to_number(letter):
return ascii_uppercase.index(letter) + 61

def find_time(relationships):
total = 0
done = []
N_WORKERS = 5
enqueued = [None] * N_WORKERS

while len(done) < len(relationships):
print(enqueued)
for i, value in enumerate(enqueued):
if value:
letter, time = value
if time == 1:
done.append(letter)
enqueued[i] = None
else:
enqueued[i] -= 1
for node in relationships:
letters_enqueued = [i for i in enqueued if i]
if node not in done and node not in letters_enqueued and relationships[node].issubset(done) and None in enqueued:
enqueued[enqueued.index(None)] = [node, letter_to_number(node)]
total += 1  