


var CharGenerator0(){
	return (char)rand();
}



var GeneratorPairArraysData(n){
	dataS = (set){};
	data = {};
	while(dataS.size()<n*2){
		x = CharGenerator0();
		if(dataS.insert(x))data.push(x);
		}
	pair = {data.mid(0,n),data.mid(n,n*2)};
	return pair;
}




//-------------------------------------------------------------------------------------------------
var CharToString(c){
	s = "";
	c = (int)c;
	i = 0;
	while(i<8){
		s += (c&(1<<7))?"1":"0";
		c = c<<1;
		++i;
		}
	return s;
}



var Analize1(Row){
	i = 0;
	size = Row.size();
	R = {};
	for(;i<size;++i)R.push(CharToString(Row[i]));
	return R;
}



var StatisticA1(Row){
	size = Row.size();
	size1 = Row[0].size();
	Result = {};
	i = 0;
	for(;i<size1;++i){
		j = 0;
		n = 0;
		for(;j<size;++j){
			if(Row[j][i]=='1')++n;
			}
		Result.push(n);
		}
	return Result;
}




var PrioritetTask(SA,SB){
	Result = {};
	i = 0;
	for(;i<8;++i){
		n = SA[i] - SB[i];
		n = (n<0?-n:n);
		Result.push(n);
		}
	P = (vector)(set)Result;
	P.revers();
	ResultIT = {};
	size = P.size();
	j = 0;
	for(;j<size;++j){
		i = 0;
		for(;i<8;++i)if(Result[i]==P[j])ResultIT.push(i);
		}
	return ResultIT;
}




AnalizePair(pair){
	
	i = 0;
	size = pair[0].size();
	for(;i<size;++i)trace(CharToString(pair[0][i]));
	trace();
	i = 0;
	size = pair[1].size();
	for(;i<size;++i)trace(CharToString(pair[1][i]));
	trace();
	
	A = Analize1(pair[0]);
	B = Analize1(pair[1]);
	SA = StatisticA1(A);
	SB = StatisticA1(B);
	PT = PrioritetTask(SA,SB);
	//trace(JSON("stringify",A));
	//trace(JSON("stringify",B));
	trace(SA);
	trace(SB);
	trace(PT);

	BinaryF = (pos,PT,p_A,p_B,p_BinaryF)=>{
		n = PT[pos];
		if(p_A->empty())return 1;
		if(p_B->empty())return 0;
		SortByIF = (p_Row,n)=>{
			pair = {{},{}};
			i = 0;
			size = p_Row->size();
			for(;i<size;++i){
				ok = ((*p_Row)[i][n]=='1');
				pair[(int)!ok].push((*p_Row)[i]);
				}
			return pair;
			};
		pairA = SortByIF.run(p_A,n);
		pairB = SortByIF.run(p_B,n);
		++pos;
		a = p_BinaryF->run(pos,PT,&pairA[0],&pairB[0],p_BinaryF);
		b = p_BinaryF->run(pos,PT,&pairA[1],&pairB[1],p_BinaryF);
		return {n,a,b};
		};

	R = BinaryF.run(0,PT,&A,&B,&BinaryF);
	trace(R.export());

}



//-------------------------------------------------------------------------------------------------
void main(){
	Randomize(47);

	i = 1;
	for(;i>0;--i){
		pair = GeneratorPairArraysData(5);
		trace(pair.export());
		AnalizePair(pair);
		}

	getstring();
}

