Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Modified version of [1] to run in node.js console
- // 1: https://jsfiddle.net/B1KMusic/gu4ofjre/3/embedded/result%2Cjs/
- // Purpose: to see how high the number of independent chains in a secret santa drawing can get.
- function FY_sort(list){
- let len = list.length;
- let sel;
- while(len){
- sel = Math.floor(Math.random() * len--);
- [list[len], list[sel]] = [list[sel], list[len]];
- }
- return list;
- }
- function exclusive_pop(list, exclude){
- let candidate = list.pop();
- if(candidate !== exclude)
- return candidate;
- list.unshift(candidate);
- return list.pop();
- }
- function getchain(list){
- let head = list.shift();
- let out = head.slice(0);
- let candidate = null;
- while((candidate = list.find(x => x[0] === out[out.length-1])) !== undefined){
- out.push(candidate[1]);
- list.splice(list.indexOf(candidate), 1);
- }
- return out;
- }
- function write_chains(list){
- let newlist = list.slice(0);
- let chains = new Array;
- while(newlist.length > 0)
- chains.push(getchain(newlist));
- console.log(`Chains: ${chains.length} / ${list.length}`);
- return chains.length;
- }
- function runit(names){
- let l1 = names.split(",");
- let l2 = FY_sort(l1.slice(0));
- let l3 = new Array;
- l1.forEach(function(el){
- l3.push([el, exclusive_pop(l2, el)]);
- });
- return write_chains(l3);
- }
- const max = (a,b) => (a > b ? a : b);
- (function(){
- let highest = 0;
- let arr = "abcdefghijklmnopqrstuvwxyz".split("");
- for(let i = 0, l; l = arr.length, i < 6; i++)
- arr.forEach(function(el){
- arr.push(el+arr[l-1]);
- });
- for(let i = 0; i < 1000; i++)
- highest = max(highest, runit(arr.join(",")));
- console.log(`Highest number of chains after 1,000 runs: ${highest}`);
- }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement