Covid chair spacing iteration challenge

A variant with a better result (permutation of the first element to be tested)

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *

def nextSpaceChair(group = [], elemA = None, startIdx = 0):
    global margin
    group = list(group)
    if elemA is None: 
        elemA = group.pop(startIdx)
        return [elemA] + nextSpaceChair(group, elemA )
    else:   
        locPt = elemA.Location.Point
        lstPair = [[x, x.Location.Point.DistanceTo(locPt)] for x in group if x.Location.Point.DistanceTo(locPt) > margin]
        lstPair.sort(key = lambda x : x[1])
        if len(lstPair) > 0:
            #get the nearest chair >= 1.50m
            nearElem = lstPair[0][0]
            #get the rest
            group = [x[0] for x in lstPair[1:] ]
            return [nearElem] + nextSpaceChair(group = group, elemA = nearElem )
        else:
            return []   

allGroup = list(UnwrapElement(IN[0]))
margin = IN[1] * 0.00328084 #simple convert in feet

out = []
for g in allGroup:
    # test with change first element, for each chair in group
    testLst = [nextSpaceChair(group = g, elemA = None, startIdx = i) for i in range(len(g))]
    maxlst = max(testLst, key = len)
    out.append(maxlst)


OUT = out
4 Likes