Move curve to different height

I have a simple question but I just can’t figure it out. So I have rectangle (blue) which I want to move to different height (min 10th contour - max. the height of the square pink/red) in the way that blue rectangles don’t overlap with each other and have one contour different between them. I tried few things (one of the closest definitions is in attached script on blue background) however it always moves 2 of them to the same height… do someone can help please?
Example what I want to get:

What I’m getting: (57.7 KB)