Is my logic crazy? I should be able to deque from free branch ( node[0] ) and power up all the connected nodes from there and get the right answer? It works on the example.
thicks = [0] * 100
energy = [0] * 100
adj = defaultdict(set)
curr = 0
for line in data.strip().splitlines():
if not line.startswith("-"):
k,v = [int(x) for x in line[:-1].split(" ") if x.isnumeric()]
thicks[k] = v
curr_node = k
else:
line = line.replace("free branch", "0")
nei, thickness = [int(x) for x in line.split(" ") if x.isnumeric()]
adj[nei].add((curr_node, thickness))
q = deque()
q.append((1, 0))
while q:
power, node = q.popleft()
for nei, thickness in adj[node]:
energy[nei] += power * thickness
if energy[nei] >= thicks[nei]:
q.append((energy[nei], nei))
print(max(energy))
#>>> thicks
#[0, 1, 1, 1, 17, 24, 15, 10, 0, 0]
#>>> energy
#[0, 1, 1, 1, 18, 24, 14, 774, 0, 0]