Skip to content

Instantly share code, notes, and snippets.

@jamesgary
Created December 22, 2012 23:04
Show Gist options
  • Save jamesgary/4361713 to your computer and use it in GitHub Desktop.
Save jamesgary/4361713 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'gmail'
class SecretSanta
class << self
def get_secret_santas(people, couples)
invalid = true
while(invalid) do
invalid = false
santas = create_possible_matches(people)
santas.each do |giver, receiver|
if giver == receiver ||
giver_and_receiver_are_couples?(giver, receiver, couples)
invalid = true
end
end
end
santas
end
private
def create_possible_matches(people)
matches = Hash[people.shuffle.zip(people.shuffle)]
end
def giver_and_receiver_are_couples?(giver, receiver, couples)
couples.each do |couple|
return true if couple.include?(giver) && couple.include?(receiver)
end
false
end
end
end
######################
people = %w{ James Suzanne Devin Timorah Alex Michelle Nick Kenneth }
couples = [
%w{ James Suzanne },
%w{ Devin Timorah },
%w{ Alex Michelle },
%w{ Alex Nick }
]
santas = SecretSanta.get_secret_santas(people, couples)
p santas
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment