Created
April 29, 2014 19:09
-
-
Save refaelos/11409118 to your computer and use it in GitHub Desktop.
People in meetings per day
This file contains hidden or 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
var peeps = [ | |
"Jieling", | |
"Claudio", | |
"Sofia", | |
"Georgy", | |
"Mitch", | |
"Antón", | |
"Daniel", | |
"Morten", | |
"Anthony", | |
"Jong", | |
"Hadar", | |
"Felipe", | |
"Flo", | |
"Ignacio", | |
"Markel", | |
"McCurdy", | |
"Andrey", | |
"Miki", | |
"David", | |
"Ryan", | |
"Rifad", | |
"Swapnil" | |
]; | |
var n=22; | |
var calendar = []; | |
function fillArrayWithNumbers(n) { | |
var arr = Array.apply(null, Array(n)); | |
return arr.map(function (x, i) { return i }); | |
} | |
Array.prototype.min = function() { | |
if (this.length === 0) { | |
return null; | |
} | |
return Math.min.apply(null, this); | |
}; | |
var setSingleMeeting = function(p1, p2, row, col) { | |
for (var i=(row-1); i>=0; i--) { | |
for (var j=0; j<(n/2); j++) { | |
var pPair = calendar[i][j]; | |
if (pPair && | |
((pPair[0] == p1 && pPair[1] == p2) || | |
(pPair[1] == p1 && pPair[0] == p2)) | |
) { | |
return false; | |
} | |
} | |
} | |
calendar[row][col] = [p1, p2]; | |
return true; | |
}; | |
var setDailyMeetings = function(people, day, col) { | |
if (people.length == 0) { | |
return true; | |
} | |
var minP1 = people.min(); | |
people.splice(people.indexOf(minP1), 1); | |
var tmpPeople1 = []; | |
while(minP1 != null) { | |
var tmpPeople2 = [], | |
people2 = people.slice(0); | |
var minP2 = people2.min(); | |
people2.splice(people2.indexOf(minP2), 1); | |
while (minP2 != null) { | |
while (minP2 && (!setSingleMeeting(minP1, minP2, day, col))) { | |
tmpPeople2.push(minP2); | |
minP2 = people2.min(); | |
people2.splice(people2.indexOf(minP2), 1); | |
} | |
if (!minP2) { | |
return false; | |
} | |
if (setDailyMeetings(people2.concat(tmpPeople2).concat(tmpPeople1), day, col + 1)) { | |
return true; | |
} else { | |
tmpPeople2.push(minP2); | |
minP2 = people2.min(); | |
people2.splice(people2.indexOf(minP2), 1); | |
} | |
} | |
tmpPeople1.push(minP1); | |
minP1 = people.min(); | |
people.splice(people.indexOf(minP1), 1); | |
} | |
return false; | |
}; | |
var solve = function() { | |
for (var i=0; i<(n-1); i++) { | |
var people = fillArrayWithNumbers(n); | |
calendar[i] = []; | |
setDailyMeetings(people, i, 0); | |
} | |
for (var i=0; i<(n-1); i++) { | |
var str = ""; | |
for (var j=0; j<(n/2); j++) { | |
str += peeps[calendar[i][j][0]] + "+" + peeps[calendar[i][j][1]] + " / "; | |
} | |
console.log(str); | |
} | |
}; | |
solve(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment