10/27
Part 3 of 7 · The shape of the treeLearn and do
File each answer by child, then read them in list order
Level 2 asks the root for the whole tree as a string: a machine's id, then its children's shapes in parentheses, in the order of its children list. A machine with no children is just its id. The five-machine tree is 0(1(3,4),2).
Each machine builds its own piece from its children's pieces, the same way it adds their counts. The trap is the order. Answers arrive in any order, and a dict keeps keys in the order they went in. So replies.values() gives the shapes in arrival order:
replies = {}
replies[4] = "4" # first
replies[3] = "3(9)"
print(",".join(replies.values()))
print(",".join(replies[child] for child in [3, 4]))
4,3(9)
3(9),4
The first line changes with the network. The second follows the list, so it's the same every time. File by child, then read in list order.
Machine 7 has children [9, 3]. Machine 3 answers first. What shape should 7 send up?
Pick an answer above to see the rest