Skip to content

Instantly share code, notes, and snippets.

@bigonese
Last active November 22, 2017 20:03
Show Gist options
  • Save bigonese/eab432a51ab44cb22e4dcb681f1b4bbe to your computer and use it in GitHub Desktop.
Save bigonese/eab432a51ab44cb22e4dcb681f1b4bbe to your computer and use it in GitHub Desktop.
Drawing names for Christmas gifts
kids = [
# Name, Family
['Noah', :esb],
['Galen', :esb],
['Charlotte', :kab],
['Alice', :kab],
['Elijah', :dkw],
['Celia', :dkw],
['Helena', :dkw]
]
givers = kids.shuffle
receivers = kids.shuffle
givers.each do |giver|
receivers.shuffle! # For good measure
# Next giver
giver_name, giver_family = giver
i = 0
begin
i+=1
# Get the next receiver
receiver_name, receiver_family = receivers.shift
if giver_family == receiver_family
# Same family, add the receiver back on the end of the list
receivers.push [receiver_name, receiver_family]
# If we've been doing this over and over again, it's because there's a problem
raise "Oops, no one left outside the giver's family! Try again." if i == 99
else
# Different family, the match is good!
break
end
end while i < 100
puts "#{giver_name} is giving to #{receiver_name}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment