Ejemplo n.º 1
0
        // Correct Answer = 109
        protected override void SolvePartTwo()
        {
            var validator = new PassportValidator();

            validator.ClearRequiredFields();

            // Birthday - four digits; at least 1920 and at most 2002.
            validator.AddRequiredField("byr", "^(19[2-9]\\d)|(200[012])$");

            // Issue Year - four digits; at least 2010 and at most 2020.
            validator.AddRequiredField("iyr", "^20(1\\d|20)$");

            // Expiry Year - four digits; at least 2020 and at most 2030.
            validator.AddRequiredField("eyr", "^20(2\\d|30)$");

            // Height - a number followed by either cm or in:
            // If cm, the number must be at least 150 and at most 193.
            // If in, the number must be at least 59 and at most 76.
            var pattern_cm = "1([5-8]\\d|9[0-3])cm";
            var pattern_in = "(59|6\\d|7[0-6])in";

            validator.AddRequiredField("hgt", $"^({pattern_cm})|({pattern_in})$");

            // Hair Colour - a # followed by exactly six characters 0-9 or a-f.
            validator.AddRequiredField("hcl", "^#[0-9a-f]{6}$");

            // Eye Colour - Exactly one of amb blu brn gry grn hzl oth
            validator.AddRequiredField("ecl", "^amb|blu|brn|gry|grn|hzl|oth$");

            // Passport ID - a nine-digit number, including leading zeroes.
            validator.AddRequiredField("pid", "^\\d{9}$");

            // Country - Optional
            validator.SetOptionalFields("cid");

            var nrValid = 0;

            foreach (var p in _passports)
            {
                if (validator.IsValid(p))
                {
                    nrValid++;
                }
            }

            Console.WriteLine($"Answer: {nrValid} valid passports");
        }
Ejemplo n.º 2
0
        // Correct Answer = 182
        protected override void SolvePartOne()
        {
            var validator = new PassportValidator();

            validator.SetRequiredFields("byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid");
            validator.SetOptionalFields("cid");

            var nrValid = 0;

            foreach (var p in _passports)
            {
                if (validator.IsValid(p))
                {
                    nrValid++;
                }
            }

            Console.WriteLine($"Answer: {nrValid} valid passports");
        }