Skip to content

Instantly share code, notes, and snippets.

@jmmastey
Last active August 29, 2015 14:11
Show Gist options
  • Save jmmastey/08c5014a1c88c095f3a8 to your computer and use it in GitHub Desktop.
Save jmmastey/08c5014a1c88c095f3a8 to your computer and use it in GitHub Desktop.
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