def connect_to_chunks(connect_to, existing_connections, steps, building_chunks):
	_connect_layers = {}
	_all_chunk_keys = []
	
	for room_name in existing_connections:
		for chunk_key in existing_connections[room_name]['chunk_keys']:
			_all_chunk_keys.append(chunk_key)
	
	_allowed = False
	for room_name in connect_to:
		if not room_name in existing_connections:
			continue
		
		_neighbors = []
		for chunk_key in existing_connections[room_name]['chunk_keys']:
			_temp_neighbors = get_neighbors(chunk_key, only_chunk_keys=building_chunks, avoid_chunk_keys=_all_chunk_keys)
			
			if not _temp_neighbors:
				return -1
			
			_neighbors.extend(_temp_neighbors)
		
		_connect_layers[room_name] = {'chunk_keys': existing_connections[room_name]['chunk_keys'],
		                              'neighbors': _neighbors}
		
		if _neighbors:
			_allowed = True
	
	if not _allowed:
		return -1
	
	_common_neighbors = {}
	_highest = 0
	for layer in _connect_layers.values():
		for chunk_key in layer['neighbors']:
			if chunk_key in _common_neighbors:
				_common_neighbors[chunk_key] += 1
			else:
				_common_neighbors[chunk_key] = 1
			
			if _common_neighbors[chunk_key]>_highest:
				_highest = _common_neighbors[chunk_key]

	for chunk_key in _common_neighbors.keys():
		if _common_neighbors[chunk_key]<_highest:
			del _common_neighbors[chunk_key]
	
	if not _common_neighbors:
		return False
	
	if _highest<len(_connect_layers):
		return -1
	
	return random.choice(_common_neighbors.keys())
dataset/ETHPy150Open flags/Reactor-3/buildinggen.py/connect_to_chunks/original