Rooms And Overlapping Elements Logic Problem

I am having a super challenging time trying to figure out a solution to this logic problem. I have a bunch of rooms in the project with several instances of casework inside those rooms. It’s easy to get the count of the elements in each room; however, I have a few tricky situations. First, the elements need to be numbered using the Mark parameter and each item of casework needs to be numbered from 1 to the total number of within the room. The tricky part is that some casework splits between 2, 3, and 4 rooms. Those items of casework that split between rooms needs to share the same number. So if one item goes between 2 rooms then the Mark number needs to be low enough that if a room has less than the total count of another room that the casework that splits between rooms doesn’t have a number that doesn’t fit in the room with less casework. Hard to explain a bit.

So what have I been able to work out. I can get the lists of casework that are split between rooms and get them properly associated to the rooms. I can force number the elements, but without any clean logic. What I have been initially trying to do is separate the casework that splits between rooms (many instances of this) from the casework that clearly sits inside the rooms. I then want to number the split casework according to the count of total casework in the room first, then take the non-split elements and number them second with the numbers that are left. I was thinking to store this into an excel file so that when the script is run again after design changes have been made that it can reference what has already been numbered and fill in the gaps or adjust accordingly.

Maybe I am thinking about this wrong and would love to get some ideas if there is a better way. I was thinking python or design script coding might make this easier except that I don’t know programming all that well.

I was able to separate the elements that were splitting between rooms from the ones that weren’t, but I have no idea how to relate the true/false lists back to the original list that has the rooms and casework together.

Thanks in advance!