Advertisement
Guest User

SecretSanta2

a guest
Nov 23rd, 2014
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. class Participant {
  2. let name: String
  3. weak var givesGiftTo: Participant?
  4. weak var pair: Participant?
  5.  
  6. init(name: String, pair: Participant?) {
  7. self.name = name
  8. self.pair = pair
  9. pair?.pair = self
  10. }
  11. }
  12.  
  13. extension NSMutableSet {
  14. func takeRandomParticipant(#thatIsNotParticipant: Participant?, forParticipant participant: Participant) -> Participant {
  15. let poolForParticipant = self.mutableCopy() as NSMutableSet
  16. poolForParticipant.removeObject(participant)
  17.  
  18. if let thatIsNotParticipant = thatIsNotParticipant {
  19. poolForParticipant.removeObject(thatIsNotParticipant)
  20. }
  21.  
  22. let index = arc4random_uniform(UInt32(poolForParticipant.count))
  23. let participant = poolForParticipant.allObjects[Int(index)] as Participant
  24. self.removeObject(participant)
  25. return participant
  26. }
  27. }
  28.  
  29. class Pool {
  30. var participants: [Participant]
  31. let pool: NSMutableSet
  32. init(participants: [Participant]) {
  33. self.participants = participants
  34. self.pool = NSMutableSet(array: participants)
  35.  
  36. for participant in participants {
  37. participant.givesGiftTo = self.pickForParticipant(participant)
  38. }
  39. }
  40.  
  41. func pickForParticipant(participant: Participant) -> Participant {
  42. return pool.takeRandomParticipant(thatIsNotParticipant: participant.pair, forParticipant: participant)
  43. }
  44.  
  45. func printResult() {
  46. for participant in self.participants {
  47. println("\(participant.name) dāvina dāvanu \(participant.givesGiftTo!.name)")
  48. }
  49. }
  50. }
  51.  
  52. let janis = Participant(name: "Jānis", pair: nil)
  53. let liga = Participant(name: "Līga", pair: janis)
  54. let adams = Participant(name: "Ādams", pair: nil)
  55. let ieva = Participant(name: "Ieva", pair: adams)
  56. let juris = Participant(name: "Juris", pair: nil)
  57. let zane = Participant(name: "Zane", pair: juris)
  58. let andrejs = Participant(name: "Andrejs", pair: nil)
  59. let baiba = Participant(name: "Baiba", pair: nil)
  60. let andris = Participant(name: "Andris", pair: nil)
  61. let maija = Participant(name: "Maija", pair: nil)
  62. let marite = Participant(name: "Mārīte", pair: nil)
  63. let sandra = Participant(name: "Sandra", pair: nil)
  64. let maris = Participant(name: "Māris", pair: nil)
  65. let ruta = Participant(name: "Ruta", pair: nil)
  66. let emils = Participant(name: "Emīls", pair: nil)
  67.  
  68. let pool = Pool(
  69. participants: [
  70. janis,
  71. liga,
  72. adams,
  73. ieva,
  74. juris,
  75. zane,
  76. andrejs,
  77. baiba,
  78. andris,
  79. maija,
  80. marite,
  81. sandra,
  82. maris,
  83. ruta,
  84. emils
  85. ]
  86. )
  87.  
  88. pool.printResult()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement