var end: Int func hasOverlap(Show) -> Bool mutating func add(Show) mutating func merge(Block) } var blocks: Block = [] for stage in stages { for show in stage.shows { let indices = blocks.filterAndMapIndex { block in block.hasOverlap(show) } if indices.isEmpty { blocks.append(newBlock(show)) } else { var mergedBlock = blocks[indices[0]] mergedBlock.add(show) for i in 1..<indices.count { mergedBlock.merge(blocks.remove(at: indices[i])) } } }