Skip to content

Instantly share code, notes, and snippets.

@hatched
Created December 9, 2013 15:18
Show Gist options
  • Save hatched/7873759 to your computer and use it in GitHub Desktop.
Save hatched/7873759 to your computer and use it in GitHub Desktop.
Javascript solution to the Secret Santa Dart Dare: https://plus.google.com/u/0/118397406534237711570/posts/XjgFRvqtVA4 ""Your Dart program must take a list of people (first name, last name) and return pairs of Secret Santas. A person can't be their own santa, and a person can't be a santa for someone with the same last name.""
var people = [
'Zoe Washburne',
'Hoban Washburne',
'Malcolm Reynolds',
'Simon Tam',
'River Tam',
'Buffy Summers',
'Dawn Summers'
];
var pairs = [],
gifted = [],
numPeople = people.length,
alreadyGifted = false,
loopCount = 0,
i = 0;
function findRecipient(person, index) {
for (i = index; i < numPeople; i++) {
var person2 = people[i];
alreadyGifted = gifted.some(function(idx) { return idx === i; } );
if (person !== person2 && !alreadyGifted) {
if (person.split(' ')[1] !== person2.split(' ')[1]) {
pairs.push(person + ' -> ' + person2);
gifted.push(i);
loopCount = 0;
return;
}
} else if (i == numPeople-1 && loopCount < 1) {
loopCount = 1;
findRecipient(person, 0);
return;
}
}
}
people.forEach(findRecipient);
console.dir(pairs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment