


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;
}




//-------------------------------------------------------------------------------------------------
AnalizePair(pair){

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

	Analize1 = (Row,p_CharToString)=>{
		i = 0;
		size = Row.size();
		R = {};
		for(;i<size;++i)R.push(p_CharToString->run(Row[i]));
		return R;
		};

	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;
		};

	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;
		};

	A = Analize1.run(pair[0],&CharToString);
	B = Analize1.run(pair[1],&CharToString);
	SA = StatisticA1.run(A);
	SB = StatisticA1.run(B);
	PT = PrioritetTask.run(SA,SB);

	if(1){
		i = 0;
		size = pair[0].size();
		for(;i<size;++i)trace(CharToString.run(pair[0][i]));
		trace();
		i = 0;
		size = pair[1].size();
		for(;i<size;++i)trace(CharToString.run(pair[1][i]));
		trace();
		trace(SA);
		trace(SB);
		trace(PT);
		}

	BinaryF = (pos,PT,p_A,p_B,p_BinaryF)=>{
		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;
			};
		n = PT[pos];
		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());
	sborkaF = (Row,p_sborkaF)=>{
		if(typeof(Row)!="vector")return {};
		n = Row[0];
		a = p_sborkaF->run(Row[1],p_sborkaF);
		b = p_sborkaF->run(Row[2],p_sborkaF);
		a += b;
		a.push(n);
		return a;
		};
	need = (vector)(set)sborkaF.run(R,&sborkaF);
	//trace(need.export());

	s1 = "CharToString = " + CharToString.exportFunction() + ";";
	s2 = "s = CharToString.run(c);";
	s = s1 + s2;
	i = 0;
	size = need.size();
	for(;i<size;++i){
		s1 = "ok" + need[i] + " = (s[" + need[i] + "]=='1');";
		s += s1;
		}

	ConstructorCondition = (Row,p_ConstructorCondition)=>{
		if(typeof(Row)!="vector")return (bool)Row;
		n = Row[0];
		a = p_ConstructorCondition->run(Row[1],p_ConstructorCondition);
		b = p_ConstructorCondition->run(Row[2],p_ConstructorCondition);
		isA = (typeof(a)=="bool");
		isB = (typeof(b)=="bool");
		if(isA && isB){
			s = "ok" + n;
			if(b)s = "!" + s;
			return s;
			}
		s = "(ok" + n + "?" + a + ":" + b + ")";
		return s;
		};
	s1 = ConstructorCondition.run(R,&ConstructorCondition);
	s += "ok = " + s1 + ";";
	s += "return ok;";
	F = (function)#;
	F.setNamesStart({"c"});
	F.import(PROGRAM(s));
	return F;
}



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

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

	getstring();
}

