import std.stdio; import std.algorithm; import std.array; import std.random; class SecretSanta { private string[string] santa; private string[] LIST = [ "David Jason", "Claire Jason", "Vicky Pollard", "Keith Pollard", "Ian Smith", "Maria Osawa", "Mark Kelly", "Sarah Kelly" ]; string[string] givePresents(){ assign; while(hasUnassigned){ assign; } return santa; } private bool hasUnassigned(){ return canFind(santa.values,"unknown"); } private void assign(){ writeln("attempting to assign"); foreach (name; LIST) { string[] list = LIST.filter!(n => n != name) .filter!(n => !canFind(n.split()[$-1],name)) .filter!(n => !canFind(santa.values,n)).array; string randomName = list.length == 0 ? "unknown" : list[uniform(0,$)]; santa[name] = randomName; } } } void main(){ SecretSanta secretSanta = new SecretSanta(); writeln(secretSanta.givePresents()); }