예제 #1
0
        private void CalculateValid()
        {
            var             lines             = File.ReadAllLines("input.txt");
            List <Passport> ValidPassports    = new List <Passport>();
            List <Passport> NotValidPassports = new List <Passport>();
            int             id = 0;


            string ecl = String.Empty;
            string pid = String.Empty;
            int?   eyr = null;
            string hcl = String.Empty;
            int?   byr = null;
            int?   iyr = null;
            int?   cid = null;
            string hgt = String.Empty;

            foreach (string item in lines)
            {
                string[] KeyValue = item.Split(':', ' ');
                for (int i = 0; i < KeyValue.Length; i++)
                {
                    int temp;
                    switch (KeyValue[i])
                    {
                    case "ecl":
                        ecl = KeyValue[i + 1];
                        break;

                    case "pid":
                        //int.TryParse(KeyValue[i + 1], out temp);
                        //pid = temp;
                        pid = KeyValue[i + 1];
                        break;

                    case "eyr":
                        int.TryParse(KeyValue[i + 1], out temp);
                        eyr = temp;
                        break;

                    case "hcl":
                        hcl = KeyValue[i + 1];
                        break;

                    case "byr":
                        int.TryParse(KeyValue[i + 1], out temp);
                        byr = temp;
                        break;

                    case "iyr":
                        int.TryParse(KeyValue[i + 1], out temp);
                        iyr = temp;
                        break;

                    case "cid":
                        int.TryParse(KeyValue[i + 1], out temp);
                        cid = temp;
                        break;

                    case "hgt":
                        hgt = KeyValue[i + 1];
                        break;

                    default:
                        break;
                    }
                }


                if (item == "")
                {
                    id++;
                    Passport newPass = new Passport(id, ecl, pid, eyr, hcl, byr, iyr, cid, hgt);
                    List <ValidationResult> validationResults = IsValidPass(newPass);

                    if (validationResults.Count != 0)
                    {
                        NotValidPassports.Add(newPass);
                    }
                    else
                    {
                        ValidPassports.Add(newPass);
                    }



                    ecl = String.Empty;
                    pid = String.Empty;
                    eyr = null;
                    hcl = String.Empty;
                    byr = null;
                    iyr = null;
                    cid = null;
                    hgt = String.Empty;
                }
                //Console.WriteLine(item);
            }
            id++;
            Passport finalPass = new Passport(id, ecl, pid, eyr, hcl, byr, iyr, cid, hgt);
            List <ValidationResult> validationResults2 = IsValidPass(finalPass);

            if (validationResults2.Count != 0)
            {
                NotValidPassports.Add(finalPass);
            }
            else
            {
                ValidPassports.Add(finalPass);
            }
            ecl = String.Empty;
            pid = String.Empty;
            eyr = null;
            hcl = String.Empty;
            byr = null;
            iyr = null;
            cid = null;
            hgt = String.Empty;

            Console.WriteLine("Not valid passwords: " + NotValidPassports.Count);
            foreach (var item in NotValidPassports)
            {
                Console.WriteLine("\n ID: " + item.ID + "\n ECL: " + item.ECL + "\n PID: " + item.PID + "\n EYR: " + item.EYR + "\n HCL: " +
                                  item.HCL + "\n BYR: " + item.BYR + "\n IYR: " + item.IYR
                                  + "\n CID: " + item.CID + "\n HGT: " + item.HGT + "\n");
            }
            Console.ReadKey();
            Console.Clear();

            Console.WriteLine("Valid passwords: " + ValidPassports.Count);
            foreach (var item in ValidPassports)
            {
                Console.WriteLine("\n ID: " + item.ID + "\n ECL: " + item.ECL + "\n PID: " + item.PID + "\n EYR: " + item.EYR + "\n HCL: " +
                                  item.HCL + "\n BYR: " + item.BYR + "\n IYR: " + item.IYR
                                  + "\n CID: " + item.CID + "\n HGT: " + item.HGT + "\n");
            }
            Console.ReadKey();
        }
예제 #2
0
        static void Main(string[] args)
        {
            var             fileLines = File.ReadAllLines(@"C:\Dev\AoC2020\Day4.txt").ToList();
            Passport        temp      = new Passport();
            List <Passport> list      = new List <Passport>();
            Type            myType    = typeof(Passport);
            int             result    = 0;

            foreach (var line in fileLines)
            {
                if (!String.IsNullOrEmpty(line))
                {
                    var split = line.Split(' ');
                    foreach (var part in split)
                    {
                        var          info     = part.Split(':');
                        PropertyInfo propInfo = myType.GetProperty(info[0]);
                        propInfo.SetValue(temp, info[1]);
                    }
                }
                else
                {
                    list.Add(temp);
                    temp = new Passport();
                }
            }

            foreach (var item in list)
            {
                bool valid = true;
                bool additionalValidation = true;

                valid = !String.IsNullOrEmpty(item.byr) &&
                        !String.IsNullOrEmpty(item.ecl) &&
                        !String.IsNullOrEmpty(item.eyr) &&
                        !String.IsNullOrEmpty(item.hcl) &&
                        !String.IsNullOrEmpty(item.hgt) &&
                        !String.IsNullOrEmpty(item.iyr) &&
                        !String.IsNullOrEmpty(item.pid);

                if (valid)
                {
                    additionalValidation = Convert.ToInt32(item.byr) >= 1920 && Convert.ToInt32(item.byr) <= 2002;

                    if (additionalValidation)
                    {
                        additionalValidation = Convert.ToInt32(item.iyr) >= 2010 && Convert.ToInt32(item.iyr) <= 2020;
                    }
                    if (additionalValidation)
                    {
                        additionalValidation = Convert.ToInt32(item.eyr) >= 2020 && Convert.ToInt32(item.eyr) <= 2030;
                    }
                    if (additionalValidation)
                    {
                        if (item.hgt.Contains("in"))
                        {
                            additionalValidation = Convert.ToInt32(item.hgt.Split('i')[0]) >= 59 && Convert.ToInt32(item.hgt.Split('i')[0]) <= 76;
                        }
                        else if (item.hgt.Contains("cm"))
                        {
                            additionalValidation = Convert.ToInt32(item.hgt.Split('c')[0]) >= 150 && Convert.ToInt32(item.hgt.Split('c')[0]) <= 193;
                        }
                        else
                        {
                            additionalValidation = false;
                        }
                    }
                    if (additionalValidation)
                    {
                        additionalValidation = item.hcl.Contains("#") && item.hcl.Length == 7;
                    }
                    if (additionalValidation)
                    {
                        char[] validchars = new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f' };
                        for (int i = 1; i < 7; i++)
                        {
                            if (additionalValidation)
                            {
                                additionalValidation = validchars.Contains(item.hcl[i]);
                            }
                        }
                    }
                    if (additionalValidation)
                    {
                        string[] validcolours = new string[] { "amb", "blu", "brn", "gry", "grn", "hzl", "oth" };
                        additionalValidation = validcolours.Contains(item.ecl);
                    }
                    if (additionalValidation)
                    {
                        additionalValidation = item.pid.Length == 9;
                        int ignoreMe;
                        if (additionalValidation)
                        {
                            additionalValidation = Int32.TryParse(item.pid, out ignoreMe);
                        }
                    }
                }

                if (valid && additionalValidation)
                {
                    result++;
                }
            }

            Console.WriteLine("Result: " + result.ToString());
            Console.ReadLine();
        }
예제 #3
0
        public static Passport GetPassportFromString(string passportString)
        {
            MatchCollection matches;

            Passport passport = new Passport();

            Regex byrPattern = new Regex(@"(byr:)(?<byr>([0-9]{4}))", RegexOptions.Compiled);

            matches = Regex.Matches(passportString, byrPattern.ToString());
            if (matches.Count > 0)
            {
                passport.byr = Int32.Parse(matches[0].Groups["byr"].Value);
            }

            Regex hclPattern = new Regex(@"(?<name>(hcl:#))(?<hcl>([a-fA-F0-9]{6}))", RegexOptions.Compiled);

            matches = Regex.Matches(passportString, hclPattern.ToString());
            if (matches.Count > 0)
            {
                passport.hcl = matches[0].Groups["hcl"].Value;
            }

            Regex iyrPattern = new Regex(@"(iyr:)(?<iyr>([0-9]{4}))", RegexOptions.Compiled);

            matches = Regex.Matches(passportString, iyrPattern.ToString());
            if (matches.Count > 0)
            {
                passport.iyr = Int32.Parse(matches[0].Groups["iyr"].Value);
            }

            Regex eyrPattern = new Regex(@"(eyr:)(?<eyr>([0-9]{4}))", RegexOptions.Compiled);

            matches = Regex.Matches(passportString, eyrPattern.ToString());
            if (matches.Count > 0)
            {
                passport.eyr = Int32.Parse(matches[0].Groups["eyr"].Value);
            }

            Regex hgtcmPattern = new Regex(@"(hgt:)(?<hgtcm>([0-9]{3}))(cm)", RegexOptions.Compiled);

            matches = Regex.Matches(passportString, hgtcmPattern.ToString());
            if (matches.Count > 0)
            {
                passport.hgtcm = Int32.Parse(matches[0].Groups["hgtcm"].Value);
            }

            Regex hgtinchPattern = new Regex(@"(hgt:)(?<hgtin>([0-9]{2}))(in)", RegexOptions.Compiled);

            matches = Regex.Matches(passportString, hgtinchPattern.ToString());
            if (matches.Count > 0)
            {
                passport.hgtin = Int32.Parse(matches[0].Groups["hgtin"].Value);
            }

            Regex eclPattern = new Regex(@"(ecl:)(?<ecl>([a-z]{3}))", RegexOptions.Compiled);

            matches = Regex.Matches(passportString, eclPattern.ToString());
            if (matches.Count > 0)
            {
                passport.ecl = matches[0].Groups["ecl"].Value;
            }

            Regex pidPattern = new Regex(@"(pid:)(?<pid>([0-9]*))", RegexOptions.Compiled);

            matches = Regex.Matches(passportString, pidPattern.ToString());
            if (matches.Count > 0)
            {
                passport.pid = matches[0].Groups["pid"].Value;
            }

            return(passport);
        }
예제 #4
0
        public static void Main(string[] args)
        {
            // I've added another line to the input.txt file because File.ReadAllLines ignores the last line if its whitespace or empty
            string[] input = File.ReadAllLines(Constants.filePath);

            int validPassports   = 0;
            int invalidPassports = 0;

            var passport = new Passport();

            //solution to be added
            foreach (var line in input)
            {
                if (line.Length > 0)
                {
                    string[] splits = line.Split(' ');

                    foreach (var split in splits)
                    {
                        string key   = split.Split(':')[0];
                        string value = split.Split(':')[1];

                        switch (key)
                        {
                        case "byr":
                            if (value.Length == 4)
                            {
                                if (Int32.Parse(value) >= 1920 && Int32.Parse(value) <= 2002)
                                {
                                    passport.BirthYear = value;
                                }
                            }
                            break;

                        case "iyr":
                            if (value.Length == 4)
                            {
                                if (Int32.Parse(value) >= 2010 && Int32.Parse(value) <= 2020)
                                {
                                    passport.IssueYear = value;
                                }
                            }
                            break;

                        case "eyr":
                            if (value.Length == 4)
                            {
                                if (Int32.Parse(value) >= 2020 && Int32.Parse(value) <= 2030)
                                {
                                    passport.ExpirationYear = value;
                                }
                            }
                            break;

                        case "hgt":
                            Regex rxHeight = new Regex(@"(cm)|(in){1}");

                            if (rxHeight.Matches(value).Any())
                            {
                                string heightMeasure = value.Substring(value.Length - 2);
                                int    heightValue   = Int32.Parse(value.Substring(0, value.Length - 2));

                                if ((heightMeasure == "cm" && heightValue >= 150 && heightValue <= 193) ||
                                    (heightMeasure == "in" && heightValue >= 59 && heightValue <= 76))
                                {
                                    passport.Height = value;
                                }
                            }
                            break;

                        case "hcl":
                            if (value.Substring(0, 1) == "#")
                            {
                                Regex rxHair         = new Regex(@"([a-f0-9]){6}");
                                var   hairColorValue = value.Substring(1);

                                if (hairColorValue.Length == 6 && rxHair.Matches(hairColorValue).Any())
                                {
                                    passport.HairColor = value;
                                }
                            }
                            break;

                        case "ecl":
                            Regex rxEye = new Regex(@"(amb|blu|brn|gry|grn|hzl|oth){1}");

                            if (rxEye.Matches(value).Any())
                            {
                                passport.EyeColor = value;
                            }
                            break;

                        case "pid":
                            Regex rxPassportId = new Regex(@"^([0-9]){9}$");

                            if (rxPassportId.Matches(value).Any())
                            {
                                passport.PassportId = value;
                            }
                            break;

                        case "cid":
                            passport.CountryId = value;
                            break;
                        }
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(passport.BirthYear) && !string.IsNullOrEmpty(passport.IssueYear) &&
                        !string.IsNullOrEmpty(passport.ExpirationYear) && !string.IsNullOrEmpty(passport.Height) &&
                        !string.IsNullOrEmpty(passport.HairColor) && !string.IsNullOrEmpty(passport.EyeColor) &&
                        !string.IsNullOrEmpty(passport.PassportId))
                    {
                        validPassports++;
                    }
                    else
                    {
                        invalidPassports++;
                    }

                    passport = new Passport();
                }
            }

            Console.WriteLine(string.Format("The number of valid passports is {0}", validPassports));

            // Prevents console app from closing
            Console.WriteLine("Press any key to close program");
            Console.ReadKey();
        }
예제 #5
0
        private static void Part2(string[] passports)
        {
            var count = 0;

            foreach (var passport in passports)
            {
                var items           = Regex.Split(passport, @"[\s \r\n]+");
                var currentPassport = new Passport();
                foreach (var item in items)
                {
                    if (item.Contains("byr:"))
                    {
                        var year = int.Parse(item.Split(":")[1]);
                        if (year >= 1920 && year <= 2002)
                        {
                            currentPassport.birthYear = year;
                        }
                    }
                    if (item.Contains("iyr:"))
                    {
                        var year = int.Parse(item.Split(":")[1]);
                        if (year >= 2010 && year <= 2020)
                        {
                            currentPassport.issueYear = year;
                        }
                    }
                    if (item.Contains("eyr:"))
                    {
                        var year = int.Parse(item.Split(":")[1]);
                        if (year >= 2020 && year <= 2030)
                        {
                            currentPassport.expirationYear = year;
                        }
                    }
                    if (item.Contains("hgt:"))
                    {
                        var values = item.Split(":")[1];
                        if (values.Contains("cm"))
                        {
                            var cmValue = int.Parse(values.Split("cm")[0]);
                            if (cmValue >= 150 && cmValue <= 193)
                            {
                                currentPassport.height = values;
                            }
                        }
                        else if (values.Contains("in"))
                        {
                            var cmValue = int.Parse(values.Split("in")[0]);
                            if (cmValue >= 59 && cmValue <= 76)
                            {
                                currentPassport.height = values;
                            }
                        }
                    }
                    if (item.Contains("hcl:"))
                    {
                        var reg   = new Regex("^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$");
                        var match = reg.Match(item.Split(":")[1]);
                        if (match.Success)
                        {
                            currentPassport.hairColor = item.Split(":")[1];
                        }
                    }
                    if (item.Contains("ecl:"))
                    {
                        var colors = new List <string> {
                            "amb", "blu", "brn", "gry", "grn", "hzl", "oth"
                        };
                        if (colors.Contains(item.Split(":")[1]))
                        {
                            currentPassport.eyeColor = item.Split(":")[1];
                        }
                    }
                    if (item.Contains("pid:"))
                    {
                        var value = item.Split(":")[1];
                        int intValue;
                        var output = int.TryParse(value, out intValue);
                        if (value.Length == 9 && output)
                        {
                            currentPassport.passportId = value;
                        }
                    }
                    if (item.Contains("cid:"))
                    {
                        currentPassport.countryId = int.Parse(item.Split(":")[1]);
                    }
                }
                if (currentPassport.IsValid())
                {
                    count += 1;
                }
            }
            Console.WriteLine("part 2 " + count);
        }
예제 #6
0
 static void Part1(string[] passports)
 {
     Console.WriteLine(passports.Count(x => Passport.TryParse(x)));
 }
예제 #7
0
        public static List <Passport> Parse(List <string> inp)
        {
            var passports = new List <Passport>();

            var tempPassword = new Passport();

            foreach (var item in inp)
            {
                var temp = item.Split(' ');

                foreach (var item2 in temp)
                {
                    var valuePairs = item2.Split(':');

                    switch (valuePairs[0])
                    {
                    case "byr":
                        tempPassword.Byr = int.Parse(valuePairs[1]);
                        break;

                    case "iyr":
                        tempPassword.Iyr = int.Parse(valuePairs[1]);
                        break;

                    case "eyr":
                        tempPassword.Eyr = int.Parse(valuePairs[1]);
                        break;

                    case "hgt":
                        tempPassword.Hgt = valuePairs[1];
                        break;

                    case "hcl":
                        tempPassword.Hcl = valuePairs[1];
                        break;

                    case "ecl":
                        tempPassword.Ecl = valuePairs[1];
                        break;

                    case "pid":
                        tempPassword.Pid = valuePairs[1];
                        break;

                    case "cid":
                        tempPassword.Cid = valuePairs[1];
                        break;

                    default:
                        break;
                    }
                }

                if (item.Length == 0)
                {
                    passports.Add(tempPassword);
                    tempPassword = new Passport();
                }
            }
            passports.Add(tempPassword);

            return(passports);
        }