Beispiel #1
0
        public int CountValid()
        {
            int      count = 0;
            Passport curr  = new Passport();

            string[] lines = File.ReadAllLines(FileName);
            foreach (string line in lines)
            {
                if (line.Length == 0)//Check if its valid, then make a new passport
                {
                    if (curr.IsValid())
                    {
                        count++;
                    }
                    curr = new Passport();
                }
                else
                {
                    string[] splitValues = line.Split(' ');
                    foreach (string value in splitValues)
                    {
                        string[] keyValue = value.Split(':');
                        switch (keyValue[0])
                        {
                        case "byr":
                            curr.BYR = keyValue[1];
                            break;

                        case "iyr":
                            curr.IYR = keyValue[1];
                            break;

                        case "eyr":
                            curr.EYR = keyValue[1];
                            break;

                        case "hgt":
                            curr.HGT = keyValue[1];
                            break;

                        case "hcl":
                            curr.HCL = keyValue[1];
                            break;

                        case "ecl":
                            curr.ECL = keyValue[1];
                            break;

                        case "pid":
                            curr.PID = keyValue[1];
                            break;

                        case "cid":
                            curr.CID = keyValue[1];
                            break;

                        default:
                            break;
                        }
                    }
                }//else
            }
            return(count);
        }
Beispiel #2
0
        public static int Compute()
        {
            var input = File.ReadAllLines("inputs\\day4.txt");

            var passport = new Passport();
            var count    = 0;

            for (int i = 0; i < input.Length; i++)
            {
                if (string.IsNullOrWhiteSpace(input[i]))
                {
                    if (passport.IsValid())
                    {
                        count++;
                    }
                    passport = new Passport();
                    continue;
                }
                var properties = input[i].Split(' ');
                foreach (string prop in properties)
                {
                    var val = prop.Substring(4);
                    switch (prop.Substring(0, 3))
                    {
                    case "byr":
                        passport.BirthYear = val;
                        break;

                    case "iyr":
                        passport.IssueYear = val;
                        break;

                    case "eyr":
                        passport.ExpirationYear = val;
                        break;

                    case "hgt":
                        passport.Height = val;
                        break;

                    case "hcl":
                        passport.HairColor = val;
                        break;

                    case "ecl":
                        passport.EyeColor = val;
                        break;

                    case "pid":
                        passport.PassportId = val;
                        break;

                    case "cid":
                        passport.CountryId = val;
                        break;
                    }
                }
            }
            if (passport.IsValid())
            {
                count++;
            }
            return(count);
        }