using System.Collections.Generic;
public FamilyName Family;
public List<Person> PastGiftees;
public static List<Person> People;
public static List<string> newList;
public static List<Person> Histories;
public static int maxAttempts = 50000;
public static void PopulatePeople()
People.Add(new Person() {Name = "Dana", Family = FamilyName.Young, Age = HumanType.Adult, PastGiftees = new List<Person>()});
People.Add(new Person() {Name = "Emily", Family = FamilyName.Young, Age = HumanType.Adult, PastGiftees = new List<Person>()});
People.Add(new Person() {Name = "Jarik", Family = FamilyName.Young, Age = HumanType.Child, PastGiftees = new List<Person>()});
People.Add(new Person() {Name = "Sammy", Family = FamilyName.Young, Age = HumanType.Child, PastGiftees = new List<Person>()});
People.Add(new Person() {Name = "Becca", Family = FamilyName.Young, Age = HumanType.Child, PastGiftees = new List<Person>()});
People.Add(new Person() {Name = "Spencer", Family = FamilyName.Young, Age = HumanType.Child, PastGiftees = new List<Person>()});
People.Add(new Person() {Name = "Jason", Family = FamilyName.Garner, Age = HumanType.Adult, PastGiftees = new List<Person>()});
People.Add(new Person() {Name = "Leann", Family = FamilyName.Garner, Age = HumanType.Adult, PastGiftees = new List<Person>()});
People.Add(new Person() {Name = "Nicci", Family = FamilyName.Garner, Age = HumanType.Child, PastGiftees = new List<Person>()});
People.Add(new Person() {Name = "Charlie", Family = FamilyName.Garner, Age = HumanType.Child, PastGiftees = new List<Person>()});
People.Add(new Person() {Name = "Candi", Family = FamilyName.Garner, Age = HumanType.Child, PastGiftees = new List<Person>()});
People.Add(new Person() {Name = "Chelsea", Family = FamilyName.Garner, Age = HumanType.Child, PastGiftees = new List<Person>()});
People.Add(new Person() {Name = "Corby", Family = FamilyName.Garner, Age = HumanType.Child, PastGiftees = new List<Person>()});
People.Add(new Person() {Name = "Jared", Family = FamilyName.Garlick, Age = HumanType.Adult, PastGiftees = new List<Person>()});
People.Add(new Person() {Name = "Carrie", Family = FamilyName.Garlick, Age = HumanType.Adult, PastGiftees = new List<Person>()});
People.Add(new Person() {Name = "Hazel", Family = FamilyName.Garlick, Age = HumanType.Child, PastGiftees = new List<Person>()});
People.Add(new Person() {Name = "Mabel", Family = FamilyName.Garlick, Age = HumanType.Child, PastGiftees = new List<Person>()});
foreach (var person in People)
foreach (var history in HistoricOrders)
int index = history.IndexOf(person.Name);
if (People.Any(x => x.Name.Equals(history[index+1])))
Person personInHistory = People.First(x => x.Name.Equals(history[index+1]));
person.PastGiftees.Add(personInHistory);
public static List<string> Order_2020 = new List<string>() {"Dana","Candi","Carrie","Leann","Hazel","Kodiak","Jason","Emily","Calvin","Jarik","Chelsea","Sammy","Corby","Jared","Charlie","Becca","Nicci","Spencer","Dana"};
public static List<string> Order_2021 = new List<string>() {"Carrie","Chelsea","Emily","Nicci","Jared","Sammy","Corby","Spencer","Charlie","Dana","Calvin","Becca","Candi","Hazel","Leann","Jarik","Jason","Carrie"};
public static List<string> Order_2022 = new List<string>() {"Mabel","Becca","Jason","Hazel","Charlie","Jared","Candi","Dana","Corby","Carrie","Jarik","Leann","Sammy","Calvin","Emily","Chelsea","Spencer","Nicci","Mabel"};
public static List<string> Order_2023 = new List<string>() {"Candi","Carrie","Calvin","Emily","Jared","Chelsea","Mabel","Jarik","Corby","Hazel","Jason","Becca","Charlie","Spencer","Leann","Dana","Nicci","Sammy","Candi"};
public static List<string> Order_2024 = new List<string>() {"Jared","Nicci","Hazel","Spencer","Candi","Becca","Corby","Jarik","Carrie","Charlie","Mabel","Emily","Jason","Sammy","Chelsea","Dana","Leann","Jared"};
public static List<List<string>> HistoricOrders = new List<List<string>>() { Order_2020, Order_2021, Order_2022, Order_2023, Order_2024 };
public static void Main()
int numberOfListsToGenerate = 5;
for (int i = 0; i < numberOfListsToGenerate && attempts < maxAttempts; i++)
Random randomNumberGenerator = new Random();
if (CreateList(randomNumberGenerator))
public static void PrintList(List<string> myList)
foreach (var name in myList)
public static void InitializeLists()
People = new List<Person>();
newList = new List<string>();
public static bool CreateList(Random rand)
Person firstPerson = PickAvailablePerson(rand);
newList.Add(firstPerson.Name);
Person currentPerson = firstPerson;
Person previousPerson = currentPerson;
while (attemptCount++ < maxAttempts && People.Any(x => !newList.Contains(x.Name)))
if (!TryPickAvailablePerson(rand, previousPerson, out currentPerson))
if (!IsValidGiftee(previousPerson, currentPerson))
newList.Add(currentPerson.Name);
previousPerson = currentPerson;
if (CountAvailablePeople() == 0 && IsValidGiftee(currentPerson, firstPerson))
newList.Add(firstPerson.Name);
public static int CountAvailablePeople()
return People.Count(x => !newList.Contains(x.Name));
public static Person PickAvailablePerson(Random rand)
var availablePeople = People.Where(x => !newList.Contains(x.Name)).ToList();
return availablePeople[rand.Next() % availablePeople.Count];
public static bool TryPickAvailablePerson(Random rand, Person Giver, out Person Receiver)
var availablePeople = People.Where(x => !newList.Contains(x.Name) && x.Family != Giver.Family)?.ToList();
if (availablePeople == null || availablePeople.Count == 0)
Receiver = availablePeople[rand.Next() % availablePeople.Count];
public static bool IsValidGiftee(Person Giver, Person Receiver)
if (Giver.Family == Receiver.Family)
if (Giver.PastGiftees.Contains(Receiver))