Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApplication26
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. //create a list names and import from data file
  15. //add parse and import function here
  16. List<Santa> Names = new List<Santa>()
  17. {
  18. new Santa("Luke Skywalker", "<luke@theforce.net>" ) ,
  19. new Santa("Leia Skywalker", "<leia@therebellion.org>") ,
  20. new Santa("Toula Portokalos" , "<toula@manhunter.org>" ) ,
  21. new Santa("Gus Portokalos" , "<gus@weareallfruit.net> " ) ,
  22. new Santa("Bruce Wayne" , "<bruce@imbatman.com>" ) ,
  23. new Santa("Virgil Brigman" , "<virgil@rigworkersunion.org>") ,
  24. new Santa("Lindsey Brigman" , "<lindsey@iseealiens.net>" )
  25. };
  26. //final secret santa list must be declared before redraw loop
  27. Dictionary<Santa, Santa> SecretSanta = new Dictionary<Santa, Santa>();
  28.  
  29. //duplicate the names list and call it drawFrom
  30. List<Santa> drawFrom = new List<Santa>();
  31. bool reDraw = true;
  32. while (reDraw)
  33. {
  34. //start with a clean Secret Santa list for each draw
  35. SecretSanta.Clear();
  36. //start with a clean drawfrom list for each draw
  37. drawFrom.Clear();
  38. //populate the drawfrom list for each draw or redraw
  39. foreach (Santa s in Names)
  40. {
  41. drawFrom.Add(s);
  42. }
  43. //for each person on names, match with a random person on the drawFrom list
  44.  
  45. Random rnd = new Random();
  46. foreach (Santa i in Names)
  47. {
  48. int match = rnd.Next(0, drawFrom.Count);
  49. //if the name is the same, draw again
  50. while (i.Name == drawFrom[match].Name)
  51. {
  52. match = rnd.Next(0, drawFrom.Count);
  53. //if the last name in the draw list matches the last name in santa list, then re-do the draw
  54. if (drawFrom.Count == 1 && i.Name == drawFrom[0].Name)
  55. {
  56. reDraw = true;
  57. }
  58. else
  59. reDraw = false;
  60.  
  61. }
  62.  
  63. //save the pairs of names in a dictionary of secret santas
  64. SecretSanta.Add(i, drawFrom[match]);
  65. //once a name is successfully drawn, remove it from the second list and draw next name again
  66. drawFrom.Remove(drawFrom[match]);
  67. }
  68.  
  69. //export the list to another file
  70. foreach (Santa p in Names)
  71. {
  72. Santa recipient;
  73. if (SecretSanta.TryGetValue(p, out recipient))
  74. Console.WriteLine(string.Format("{0} will send a gift to {1}", p.Name, recipient.Name));
  75. }
  76. //display results as a chain
  77. Santa recipient;
  78. Santa sender = Names[0];
  79. foreach (Santa s in Names)
  80. {
  81. if (SecretSanta.TryGetValue(sender, out recipient))
  82. Console.WriteLine(string.Format("{0} will send a gift to {1}", sender.Name, recipient.Name));
  83. sender = recipient;
  84. }
  85.  
  86.  
  87. }
  88. Console.WriteLine();
  89. Thread.Sleep(2000);
  90. Console.Read();
  91.  
  92.  
  93. }
  94. public class Santa
  95. {
  96. public string Name { get; set; }
  97. public string EMail { get; set; }
  98. public Santa(string n, string e)
  99. {
  100. this.Name = n;
  101. this.EMail = e;
  102. }
  103.  
  104. }
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement