Skip to content

Instantly share code, notes, and snippets.

@quii
Created December 25, 2012 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quii/4375599 to your computer and use it in GitHub Desktop.
Save quii/4375599 to your computer and use it in GitHub Desktop.
import util.Random
/*
Takes a sequence of secret santa participants and suggests who should give to who
*/
object SecretSanta {
def main(args: Array[String]){
val people = args.toSet
if (people.size<3){
println("Secret santa with less than 3 people is silly")
sys.exit()
}
val pairs = scala.collection.mutable.Map[String, String]()
people foreach { person =>
def getPersonToGiveTo(candidates: Set[String]): Option[String] = {
def notBeingGivenYet(p: String): Boolean = !pairs.values.exists(_==p)
def notGivingToMe(p: String): Boolean = pairs.get(p) match{
case Some(receiver) if receiver==person => false
case _ => true
}
Random.shuffle(candidates filter notBeingGivenYet filter notGivingToMe).headOption
}
val peopleNotMe = people.filterNot(_ == person)
getPersonToGiveTo(peopleNotMe) match{
case Some(luckyPerson) => pairs += person -> luckyPerson
case None => {}
}
}
println("Secret santa happy times! \n")
pairs foreach { happyCouple =>
println("%s should give to %s".format(happyCouple._1, happyCouple._2))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment