Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace ConsoleApplication26
- {
- class Program
- {
- static void Main(string[] args)
- {
- //create a list names and import from data file
- //add parse and import function here
- List<Santa> Names = new List<Santa>()
- {
- new Santa("Luke Skywalker", "<luke@theforce.net>" ) ,
- new Santa("Leia Skywalker", "<leia@therebellion.org>") ,
- new Santa("Toula Portokalos" , "<toula@manhunter.org>" ) ,
- new Santa("Gus Portokalos" , "<gus@weareallfruit.net> " ) ,
- new Santa("Bruce Wayne" , "<bruce@imbatman.com>" ) ,
- new Santa("Virgil Brigman" , "<virgil@rigworkersunion.org>") ,
- new Santa("Lindsey Brigman" , "<lindsey@iseealiens.net>" )
- };
- //final secret santa list must be declared before redraw loop
- Dictionary<Santa, Santa> SecretSanta = new Dictionary<Santa, Santa>();
- //duplicate the names list and call it drawFrom
- List<Santa> drawFrom = new List<Santa>();
- bool reDraw = true;
- while (reDraw)
- {
- //start with a clean Secret Santa list for each draw
- SecretSanta.Clear();
- //start with a clean drawfrom list for each draw
- drawFrom.Clear();
- //populate the drawfrom list for each draw or redraw
- foreach (Santa s in Names)
- {
- drawFrom.Add(s);
- }
- //for each person on names, match with a random person on the drawFrom list
- Random rnd = new Random();
- foreach (Santa i in Names)
- {
- int match = rnd.Next(0, drawFrom.Count);
- //if the name is the same, draw again
- while (i.Name == drawFrom[match].Name)
- {
- match = rnd.Next(0, drawFrom.Count);
- //if the last name in the draw list matches the last name in santa list, then re-do the draw
- if (drawFrom.Count == 1 && i.Name == drawFrom[0].Name)
- {
- reDraw = true;
- }
- else
- reDraw = false;
- }
- //save the pairs of names in a dictionary of secret santas
- SecretSanta.Add(i, drawFrom[match]);
- //once a name is successfully drawn, remove it from the second list and draw next name again
- drawFrom.Remove(drawFrom[match]);
- }
- //export the list to another file
- foreach (Santa p in Names)
- {
- Santa recipient;
- if (SecretSanta.TryGetValue(p, out recipient))
- Console.WriteLine(string.Format("{0} will send a gift to {1}", p.Name, recipient.Name));
- }
- //display results as a chain
- Santa recipient;
- Santa sender = Names[0];
- foreach (Santa s in Names)
- {
- if (SecretSanta.TryGetValue(sender, out recipient))
- Console.WriteLine(string.Format("{0} will send a gift to {1}", sender.Name, recipient.Name));
- sender = recipient;
- }
- }
- Console.WriteLine();
- Thread.Sleep(2000);
- Console.Read();
- }
- public class Santa
- {
- public string Name { get; set; }
- public string EMail { get; set; }
- public Santa(string n, string e)
- {
- this.Name = n;
- this.EMail = e;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement