Sounds like the bin packing algorithm. Have a look at the below video, it should give you a good idea on how to implement this in Dynamo:
Sounds like the bin packing algorithm. Have a look at the below video, it should give you a good idea on how to implement this in Dynamo: