public SantaList GenerateSantaList(IList<Contact> contacts, IList<Restriction> restrictions)
        {
            // go through all contacts and pick another contact as the
            // order contacts by the number of restrictions they have

            var orderedByRestrictions = SortByRestrictions(contacts, restrictions).ToList();

            Random rng = new Random();

            var remainingReceivers = new List<Contact>(orderedByRestrictions);

            var santasList = new SantaList();

            foreach (var santa in orderedByRestrictions)
            {
                var withoutSelf = RemoveSelfFromList(remainingReceivers, santa);

                var restrictionsForSanta = restrictions.Where(r => r.Between.Id == santa.Id);
                var possibleReceivers = RemoveRestrictionsFromList(withoutSelf, restrictionsForSanta);

                if (possibleReceivers.Count == 0)
                    throw new Exception("No options left, try again");

                int randomIndex = rng.Next(possibleReceivers.Count);

                var randomReceiver = possibleReceivers[randomIndex];

                remainingReceivers.Remove(randomReceiver);
                var entry = new SantaEntry(santa, randomReceiver);
                santasList.AddEntry(entry);
            }

            return santasList;
        }
示例#2
0
 public virtual void AddEntry(SantaEntry entry)
 {
     SantaEntries.Add(entry);
 }