Ejemplo n.º 1
0
        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;
        }
 private static void SaveEncodedResultTo(string fullPathEncoded, SantaList result)
 {
     var encodedString = result.SantaEntries.Select(e => string.Join(",", e.Santa.Id, e.Santa.PhoneNumber, e.Receiver.Id));
     File.WriteAllLines(fullPathEncoded, encodedString);
 }
 private static void SaveDecodedResultTo(string fullPathDecoded, SantaList result)
 {
     var decodedString = result.SantaEntries.Select(e => e.Santa.NickName + " is buying for " + e.Receiver.NickName);
     File.WriteAllLines(fullPathDecoded, decodedString);
 }