Skip to content

Instantly share code, notes, and snippets.

@birkir
Last active August 29, 2015 14:10
Show Gist options
  • Save birkir/065ac234afa3de697c6d to your computer and use it in GitHub Desktop.
Save birkir/065ac234afa3de697c6d to your computer and use it in GitHub Desktop.
Jóladagatal 2/24 - Jólakveðja
/**
* Circulate santas saying merry christmas to each other
* @param int Numer of santas
* @return String
*/
function santaSaidMerryChristmas(n) {
// vars
var santas = Array.apply(null, Array(n)).map(function (_, i) { return (i + 1); }),
i = 0;
// while two santas are available
while (santas.length > 1) {
// reset cursor if last sant
i = (i >= santas.length) ? 0 : i;
// splice the next santa out
santas.splice(((i + 1) >= santas.length) ? 0 : (i + 1), 1);
// move cursor
i++
}
// pop last santa
return 'Santa ' + santas.pop() + ' was left in the circle';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment