Last active
August 29, 2015 14:11
-
-
Save jmmastey/08c5014a1c88c095f3a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def secret_santas(names) | |
groups = names.map(&:split).group_by(&:last).values.sort_by(&:length).reverse | |
santas = groups.flatten # list of names, most prolific first | |
santees = santas.clone.shuffle # random for increased fun | |
santas.map do |santa| | |
santee = santees.find { |a| a.last != santa.last } | |
raise "ran out of matches for #{name}" if santee.nil? | |
santees.delete santee | |
puts "#{santa.join(' ')} -> #{santee.join(' ')}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment