// arhiv.code




var findFunctionInArhivParadigm(var p_function,var resultF){
	module=getThisModule();
	func=p_function->copy();
	if(isset(resultF)){
		names=module.getAllFunctionNames();
		ok=1;
		n=1;
		while(ok){
			if(names.in("paradigma"+n)){
				++n;
				continue;
				}
			ok=0;
			}
		namef="paradigma"+n;
		func.setName(namef);
		module.add(func);
		namef="result"+n;
		resultF.setName(namef);
		module.add(resultF);
		return 0;
		}
	fs=module.getAllFunctions();
	i=0;
	for(;i<fs.size();++i){
		namef=fs[i].getName();
		n=((int)namef.substr("paradigma".size()));
		if(!n);else {
			func.setName(namef);
			if(func==fs[i]){
				namef="result"+n;
				f=module.FindFunction(namef);
				return f;
				}
			}
		}
	return 0;
}



void paradigma1(){
	S={0,0,0,0};
	do{
		trace(S.export());
		ControlPset(S);
		P={};
		i=0;
		for(;i<4;++i)if(S[i]==S[0])P.push(i);
		t=P[#];
		n=1-S[0];
		S[0]=S[t]=n;
		if(S[1]==S[3]&&n!=S[1])OFF;
		if(S[2]==S[3]&&n!=S[2])OFF;
		}while(S!={1,1,1,1});
	trace(S.export());
}



void result1(){
	planRandomWays={{3,0,1,2,1,0,1},{3,0,2,2,1,0,1}};
	planRandomWay=planRandomWays[#];
	S={0,0,0,0};
	do{
		trace(S.export());
		P={};
		i=0;
		for(;i<4;++i)if(S[i]==S[0])P.push(i);
		t=P[planRandomWay.pop_begin()];
		n=1-S[0];
		S[0]=S[t]=n;
		}while(S!={1,1,1,1});
	trace(S.export());
}



void paradigma2(){
	S={0,0,0,0};
	while(S!={1,1,1,1}){
		trace(S.export());
		ControlPset(S);
		Massa={0,2,1,1};
		P={0};
		sum=0;
		i=1;
		for(;i<S.size();++i)if(S[i]==S[0]){if(#%2){
			P.push(i);
			sum+=Massa[i];
			}};
		if(sum>2||sum==0)OFF;
		n=1-S[0];
		i=0;
		for(;i<P.size();++i)S[P[i]]=n;
		}
	trace(S.export());
}



void result2(){
	planRandomWays={{0,1,1,1,0,1,0,0,1,1,1},{0,1,1,0,1,1,0,0,1,1,1}};
	planRandomWay=planRandomWays[#];
	S={0,0,0,0};
	while(S!={1,1,1,1}){
		trace(S.export());
		Massa={0,2,1,1};
		P={0};
		sum=0;
		i=1;
		for(;i<S.size();++i)if(S[i]==S[0]){
			if(planRandomWay.pop_begin()%2){
				P.push(i);
				sum+=Massa[i];
				}
			};
		n=1-S[0];
		i=0;
		for(;i<P.size();++i)S[P[i]]=n;
		}
	trace(S.export());
}



void paradigma3(){
	S={1,1,1,0,2,2,2};
	while(S!={2,2,2,0,1,1,1}){
		trace(S.export());
		ControlPset(S);
		P={};
		i=0;
		for(;i<S.size();++i){
			if(S[i]==1){
				if(S.size()>i+1){if(S[i+1]==0)P.push({i,i+1});}
				if(S.size()>i+2){if(S[i+2]==0)P.push({i,i+2});}
				}
			if(S[i]==2){
				if(i-1>=0){if(S[i-1]==0)P.push({i,i-1});}
				if(i-2>=0){if(S[i-2]==0)P.push({i,i-2});}
				}
			}
		if(P.empty())OFF;
		X=P[#];
		S[X[1]]=S[X[0]];
		S[X[0]]=0;
		}
	trace(S.export());
}



void result3(){
	planRandomWays={{1,2,1,0,0,0,0,1,1,0,0,0,0,0,0},{2,0,1,1,1,1,0,0,0,0,0,1,0,0,0}};
	planRandomWay=planRandomWays[#];
	S={1,1,1,0,2,2,2};
	while(S!={2,2,2,0,1,1,1}){
		trace(S.export());
		P={};
		i=0;
		for(;i<S.size();++i){
			if(S[i]==1){
				if(S.size()>i+1){
					if(S[i+1]==0)P.push({i,i+1});
					}
				if(S.size()>i+2){
					if(S[i+2]==0)P.push({i,i+2});
					}
				}
			if(S[i]==2){
				if(i-1>=0){
					if(S[i-1]==0)P.push({i,i-1});
					}
				if(i-2>=0){
					if(S[i-2]==0)P.push({i,i-2});
					}
				}
			}
		X=P[planRandomWay.pop_begin()];
		S[X[1]]=S[X[0]];
		S[X[0]]=0;
		}
	trace(S.export());
}



void paradigma4(){
	S={{3,2,1},{},{}};
	while(S!={{},{3,2,1},{}}){
		trace(S.export());
		ControlPset(S);
		pairs={};
		i=0;
		for(;i<3;++i)if(!S[i].empty()){
			UPiz=S[i].end();
			j=0;
			for(;j<3;++j)if(j!=i){
				UPto=0;
				if(S[j].size())UPto=S[j].end();
				if(UPto==0||UPto>UPiz)pairs.push({i,j});
				};
			};
		pair=pairs[#];
		X=S[pair[0]].pop();
		S[pair[1]].push(X);
		}
	trace(S.export());
}



void result4(){
	planRandomWays={{0,0,1,0,1,2,0},{0,0,1,0,1,2,0}};
	planRandomWay=planRandomWays[#];
	S={{3,2,1},{},{}};
	while(S!={{},{3,2,1},{}}){
		trace(S.export());
		pairs={};
		i=0;
		for(;i<3;++i)if(!S[i].empty()){
			UPiz=S[i].end();
			j=0;
			for(;j<3;++j)if(j!=i){
				UPto=0;
				if(S[j].size())UPto=S[j].end();
				if(UPto==0||UPto>UPiz)pairs.push({i,j});
				};
			};
		pair=pairs[planRandomWay.pop_begin()];
		X=S[pair[0]].pop();
		S[pair[1]].push(X);
		}
	trace(S.export());
}



void paradigma5(){
	S={1,7,12,3,8};
	do{
		ok=0;
		t=S[#];
		ControlPset(t);
		i=0;
		for(;i<S.size();++i)if(S[i]>t)ok=1;
		}while(ok);
	trace(t);
}



void result5(){
	planRandomWays={{2}};
	planRandomWay=planRandomWays[#];
	S={1,7,12,3,8};
	do{
		ok=0;
		t=S[planRandomWay.pop_begin()];
		i=0;
		for(;i<S.size();++i)if(S[i]>t)ok=1;
		}while(ok);
	trace(t);
}


// arhiv.code	:-|