Last active
December 11, 2017 00:39
-
-
Save MrTrick/f4a25cfd1c786858a56d8becfd97abd0 to your computer and use it in GitHub Desktop.
Secret santa permutor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EAS=>PRB, MP=>AS, R=>E, B=>M | |
EAS=>PRB, MP=>ES, R=>A, B=>M | |
EAS=>PRB, MP=>EA, R=>S, B=>M | |
EAS=>MPR, MP=>EA, R=>B, B=>S | |
EAS=>MPB, MP=>AR, R=>E, B=>S | |
EAS=>MPR, MP=>AB, R=>E, B=>S | |
EAS=>MRB, MP=>EA, R=>P, B=>S | |
EAS=>MPR, MP=>EB, R=>A, B=>S | |
EAS=>MPB, MP=>ER, R=>A, B=>S | |
EAS=>PRB, MP=>EA, R=>M, B=>S | |
EAS=>PRB, MP=>ES, R=>M, B=>A | |
EAS=>MPR, MP=>ES, R=>B, B=>A | |
EAS=>MPR, MP=>SB, R=>E, B=>A | |
EAS=>MPB, MP=>SR, R=>E, B=>A | |
EAS=>MRB, MP=>ES, R=>P, B=>A | |
EAS=>MPR, MP=>EB, R=>S, B=>A | |
EAS=>MPB, MP=>ER, R=>S, B=>A | |
EAS=>MRB, MP=>EA, R=>S, B=>P | |
EAS=>MRB, MP=>AS, R=>E, B=>P | |
EAS=>MRB, MP=>ES, R=>A, B=>P | |
EAS=>MPB, MP=>ES, R=>A, B=>R | |
EAS=>MPB, MP=>EA, R=>S, B=>R | |
EAS=>MPB, MP=>AS, R=>E, B=>R | |
EAS=>MRB, MP=>AS, R=>P, B=>E | |
EAS=>MPB, MP=>SR, R=>A, B=>E | |
EAS=>MPR, MP=>SB, R=>A, B=>E | |
EAS=>MPR, MP=>AB, R=>S, B=>E | |
EAS=>MPB, MP=>AR, R=>S, B=>E | |
EAS=>PRB, MP=>AS, R=>M, B=>E | |
EAS=>MPR, MP=>AS, R=>B, B=>E |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* permutations(N) { | |
var arr=Array(N).fill(0).map((x,y)=>y); //Fill with 0,1,2,3,4....N-1 | |
function swap(i,j) { var tmp=arr[i]; arr[i]=arr[j]; arr[j]=tmp; } | |
var c=Array(N).fill(0); | |
yield arr.slice(); //start with first permutation | |
for(var i=0;i<N;) { //Heap's algorithm | |
if (c[i]<i) { | |
swap(i%2?c[i]:0,i); | |
yield arr.slice(); | |
c[i]++; i=0; | |
} else { | |
c[i]=0; i++; | |
} | |
} | |
} | |
var people = ['E','A','S','M','P','R','B']; | |
var fam = [0,0,0,1,1,2,3]; | |
var N = 7; | |
for (var P of permutations(N)) { | |
if (P.some((t,f)=>(f==t))) continue; //Can't give to self | |
if (P.some((t,f)=>(fam[f]==fam[t]))) continue; //Can't give to same family. | |
if (P.some((t,f)=>{ | |
//console.log("From: "+f+" To: "+t+" To-: "+P[f-1]+" Fam:"+fam[f]+"-"+fam[f-1]); | |
return f>0 && fam[f-1]==fam[f] && P[f-1]>t; | |
})) continue; //Take only first permutation of recipients from a given family | |
//Format nicely | |
var byfam=[]; | |
for(var i=0;i<N;i++) { | |
var g = fam[i]; | |
byfam[g]||(byfam[g]={f:[],t:[]}); | |
byfam[g].f.push(people[i]); | |
byfam[g].t.push(people[P[i]]); | |
} | |
console.log( byfam.map(g=>g.f.join('')+'=>'+g.t.join('')).join(', ')); | |
//console.log(P); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment