Exemplo n.º 1
0
            public void OrderTicketFields()
            {
                var validClasses = new List <KeyValuePair <string, int> >();
                var headerRows   = new Dictionary <string, int>();
                var valueCount   = ValidTickets.Max(v => v.Values.Count);

                for (var i = 0; i < valueCount; ++i)
                {
                    var columnValues = ValidTickets.Select(t => t.Values[i]);
                    validClasses.AddRange(FieldBounds.Where(fb => fb.IsRuleValidForValues(columnValues))
                                          .Select(fb => new KeyValuePair <string, int>(fb.ClassName, i)));
                }
                while (validClasses.Count > 0)
                {
                    validClasses.GroupBy(kvp => kvp.Key)
                    .Where(g => g.Count() == 1)
                    .SelectMany(g => g.Select(k => k))
                    .ToList().ForEach(kvp => headerRows.AddKvp(kvp));
                    validClasses.RemoveAll(kvp => headerRows.ContainsValue(kvp.Value));
                }

                var result = headerRows.Where(kv => kv.Key.StartsWith("departure"))
                             .Select(kv => MyTicket.Values[kv.Value])
                             .Aggregate(1L, (f, s) => f * s);
            }
Exemplo n.º 2
0
        public void FindFieldIndexes()
        {
            Dictionary <string, HashSet <int> > possibleIndexes = new Dictionary <string, HashSet <int> >();

            foreach (var rule in Rules)
            {
                var fieldName = rule.Name;
                possibleIndexes[fieldName] = new HashSet <int>();
                for (int i = 0; i < Rules.Count; i++)
                {
                    if (ValidTickets.All(ticket => rule.IsValidValue(ticket.Values[i])))
                    {
                        possibleIndexes[fieldName].Add(i);
                    }
                    else
                    {
                        if (fieldName == "type" || fieldName == "train")
                        {
                            var invalid = ValidTickets.First(ticket => !rule.IsValidValue(ticket.Values[i]));
                            Console.WriteLine($"First mismatch for {fieldName} at {i}: {invalid.Values[i]}");
                        }
                    }
                }
            }

            bool stop = false;

            while (_FieldIndexes.Count < Rules.Count && !stop)
            {
                foreach (var kvp in possibleIndexes)
                {
                    foreach (var key in _FieldIndexes.Keys)
                    {
                        kvp.Value.Remove(_FieldIndexes[key]);
                    }

                    if (kvp.Value.Count == 1)
                    {
                        _FieldIndexes[kvp.Key] = kvp.Value.First();
                    }
                    else if (kvp.Value.Count == 0)
                    {
                        stop = true;
                    }
                }

                foreach (var key in _FieldIndexes.Keys)
                {
                    possibleIndexes.Remove(key);
                }
            }
        }
Exemplo n.º 3
0
        public List <int> ValidateTickets(List <string> tickets)
        {
            List <int> invalidFields = new List <int>();

            foreach (var ticket in tickets)
            {
                var badFields = ValidateTicket(ticket);
                if (badFields.Count > 0)
                {
                    invalidFields.AddRange(badFields);
                }
                else
                {
                    ValidTickets.Add(ticket);
                }
            }
            return(invalidFields);
        }