コード例 #1
0
ファイル: Day4.cs プロジェクト: rvugt85/Advent-of-code
        public PassPortData CreatePassportData(string paragraph)
        {
            var passportData = new PassPortData {
                HeightData = new HeightData()
            };

            var keyAndValues = mainProgram.SplitParagraphsByLineEndingsAndSpaces(paragraph);

            foreach (var keyAndValue in keyAndValues)
            {
                var split = keyAndValue.Split(':');
                var key   = split[0];
                var value = split[1];
                switch (key)
                {
                case "byr":
                    passportData.BirthYear = int.Parse(value);
                    break;

                case "iyr":
                    passportData.IssueYear = int.Parse(value);
                    break;

                case "eyr":
                    passportData.ExpirationYear = int.Parse(value);
                    break;

                case "hgt":
                    passportData.HeightData = CreateHeightData(value);
                    break;

                case "hcl":
                    passportData.HairColor = value;
                    break;

                case "ecl":
                    if (Enum.TryParse(value, out EyeColor eyeColor))
                    {
                        passportData.EyeColor = eyeColor;
                    }
                    break;

                case "pid":
                    passportData.PassportId = value;
                    break;

                case "cid":
                    passportData.CountryId = int.Parse(value);
                    break;
                }
            }

            return(passportData);
        }
コード例 #2
0
ファイル: Day4.cs プロジェクト: rvugt85/Advent-of-code
        public bool PassportIsValid(PassPortData passportData)
        {
            if (passportData.BirthYear == null ||
                passportData.IssueYear == null ||
                passportData.ExpirationYear == null ||
                passportData.HeightData == null ||
                passportData.HairColor == null ||
                passportData.EyeColor == null ||
                passportData.PassportId == null)
            {
                return(false);
            }

            if (!Regex.Match(passportData.PassportId, PassportIdPattern).Success)
            {
                return(false);
            }
            if (passportData.BirthYear < 1920 || passportData.BirthYear > 2002)
            {
                return(false);
            }
            if (passportData.IssueYear < 2010 || passportData.IssueYear > 2020)
            {
                return(false);
            }
            if (passportData.ExpirationYear < 2020 || passportData.ExpirationYear > 2030)
            {
                return(false);
            }
            if (!IsHeightValid(passportData.HeightData))
            {
                return(false);
            }
            if (!Regex.Match(passportData.HairColor, HaircolorPattern).Success)
            {
                return(false);
            }

            return(true);
        }