Пример #1
0
            public void Puzzle_16B()
            {
                var fields    = BuildFields(PuzzleFields);
                var validator = new TrainTicketValidator
                {
                    Fields = fields
                };

                var validTickets = PuzzleNerbyTickets
                                   .Where(ticket => ticket.All(x => validator.ValidateTicketValue(x)))
                                   .ToList();

                validator.DetermineFieldOrder(validTickets);

                var departureValues = new Dictionary <string, (int index, int value)>();

                for (var i = 0; i < validator.Fields.Length; i++)
                {
                    if (validator.Fields[i].Name.StartsWith("departure"))
                    {
                        departureValues.Add(validator.Fields[i].Name, (i, PuzzleMyTicket[i]));
                    }
                }

                var departureProduct = 1L;

                foreach (var departure in departureValues)
                {
                    departureProduct *= departure.Value.value;
                }

                Assert.Equal(2355350878831, departureProduct);
            }
Пример #2
0
            public void Example_1(int[] ticket, bool expectedValidity, int expectedBadValue)
            {
                var validator = new TrainTicketValidator
                {
                    Fields = new[]
                    {
                        new Field
                        {
                            Name   = "class",
                            Ranges = new[] { new Range(1, 3), new Range(5, 7) }
                        },
                        new Field
                        {
                            Name   = "row",
                            Ranges = new[] { new Range(6, 11), new Range(33, 44) }
                        },
                        new Field
                        {
                            Name   = "seat",
                            Ranges = new[] { new Range(13, 40), new Range(45, 50) }
                        },
                    }
                };

                var invalidValues = ticket.Select(value => validator.ValidateTicketValue(value)
                        ? 0
                        : value)
                                    .ToList();

                var valid = !invalidValues.Any(x => x > 0);

                Assert.Equal(expectedValidity, valid);
                Assert.Contains(expectedBadValue, invalidValues);
            }
Пример #3
0
            public void Puzzle_16A()
            {
                var fields    = BuildFields(PuzzleFields);
                var validator = new TrainTicketValidator
                {
                    Fields = fields
                };

                var invalidValues = new List <int>();

                foreach (var ticket in PuzzleNerbyTickets)
                {
                    invalidValues
                    .AddRange(ticket
                              .Select(value => validator.ValidateTicketValue(value)
                                ? 0
                                : value));
                }

                Assert.Equal(18227, invalidValues.Sum());
            }
Пример #4
0
            public void Example_2()
            {
                var fields = BuildFields(new[]
                {
                    "class: 0-1 or 4-19",
                    "row: 0-5 or 8-19",
                    "seat: 0-13 or 16-19"
                });
                var validator = new TrainTicketValidator
                {
                    Fields = fields
                };

                var validTickets = new[]
                {
                    new[] { 3, 9, 18 },
                    new[] { 15, 1, 5 },
                    new[] { 5, 14, 9 }
                }
                .Where(ticket => ticket.All(x => validator.ValidateTicketValue(x)))
                .ToList();

                validator.DetermineFieldOrder(validTickets);

                var expectedOrder = new[]
                {
                    "row",
                    "class",
                    "seat"
                };

                for (var i = 0; i < validator.Fields.Length; i++)
                {
                    Assert.Equal(expectedOrder[i], validator.Fields[i].Name);
                }
            }