Skip to content

Instantly share code, notes, and snippets.

@betzerra
Created December 11, 2018 14:42
Show Gist options
  • Save betzerra/71607cc8d82315e4d64c4bae08a349b5 to your computer and use it in GitHub Desktop.
Save betzerra/71607cc8d82315e4d64c4bae08a349b5 to your computer and use it in GitHub Desktop.
secret_santa.rb
people = ARGV
possible_receivers = people.dup
result = {}
people.each do |p|
# a person doesn't have to give a gift to himself;
tmp = possible_receivers.dup
tmp.delete(p)
receiver = tmp.sample
# a person doesn't receive a gift from the person he's given a gift to;
while result[receiver].nil? == false && result[receiver] == p do
receiver = tmp.sample
end
result[p] = receiver
possible_receivers.delete(receiver)
end
puts result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment