The challenge is to create one program that will accept as arguments (arrays of arrays) or on standard input (line-by-line, separated by commas) a collection of families and names, like so:
John,Anna
Mark,James
Elizabeth,Daniel,Isabelle
Ryan,Ruth
As well as two other arguments (two more arrays of pairs, as arrays) or on standard input (a blank line after the preceding input, followed by two comma-separated lists on two lines with an =
separating pairings; see example) that represent the two previous years' Christmas lists:
John=Mark,James=Anna,Elizabeth=Ryan,...
John=Isabelle,Ryan=Elizabeth,Daniel=Mark,...
The function/program must return/output (in any format) a way that each person is assigned somebody to give a present to, following these rules:
- A person cannot give a present to him or herself.
- A person cannot give a present to anybody in his or her own family.
- A person cannot be assigned the same person he or she was assigned in any of the two previous years.
If such a matching is impossible for every person, output/return null
or nothing or something appropriate.
This is a code golf, so shortest code wins, but I'll also give a bounty to the code that runs fastest (on my computer, at least, though results shouldn't vary much).
=
to represent the assignments suggests it... \$\endgroup\$Dan=Marge,Marge=Carolyn,Carolyn=Don,Don=Bob,Bob=Dan
) \$\endgroup\$