Get object dimensions and store them in a list

There are a few such scripts out there - that would be a place to start, I suppose.
You’ll need to write some kind of solver that can iterate through several attempts.

One such method is discussed here (first google hit):