Пример #1
0
        public Dictionary <int, string> GetValidFieldPositions()
        {
            //< Get all nearby, valid tix
            var validTix = Nearby.Where(x => x.IsValid(this));

            //< Instantiate a hashset of all 'solved' notes and a map of which indices have been solved by what note
            var usedFields = new HashSet <string>();
            var positions  = new Dictionary <int, string>();

            //< Grab the ticket count so we know how values a note must cover to be a potential solution
            int ticketCount = validTix.Count();

            //< Need to solve for each position within the tickets
            while (usedFields.Count != Mine.Length)
            {
                foreach (int i in Enumerable.Range(0, Mine.Length))
                {
                    if (positions.ContainsKey(i))
                    {
                        //< Already solved - skip
                        continue;
                    }

                    //< Get all the values at this position in the ticket from nearby, valid tix
                    var vals = validTix.Select(x => x.GetValue(i));
                    //< Get the map of number of times a note covered a value in this index
                    var noteMap = GetNoteMap(vals);
                    //< Get the matching notes (notes which were covered by all tix)
                    var matching = noteMap.Where(kvp => !usedFields.Contains(kvp.Key))
                                   .Where(kvp => kvp.Value == ticketCount);
                    //< Switch on the resulting count - if more than one, need to solve other positions first
                    if (matching.Count() > 1)
                    {
                        //< Can't decide yet - retain for later check
                    }
                    else
                    {
                        //< Only one solution here - use it and move on
                        var match = matching.Single();
                        positions.Add(i, match.Key);
                        usedFields.Add(match.Key);
                    }
                }
            }

            return(positions);
        }
Пример #2
0
        public Tickets(string fields, string nearbyTickets, string myTicket)
        {
            Fields = fields
                     .SplitLines()
                     .Select(x => new Field(x))
                     .ToList();

            Nearby = nearbyTickets
                     .SplitLines()
                     .Select(x => new Ticket(x.SplitOn(',').Select(int.Parse), Fields))
                     .ToList();

            MyTicket = new Ticket(myTicket.SplitOn(',').Select(int.Parse), Fields);

            ValidTickets = new List <Ticket>(Nearby.Where(x => x.IsValid))
            {
                MyTicket
            };
        }