Beispiel #1
0
        private int CountValidPasswordsRuleTwo()
        {
            int validPasswordCount = 0;

            foreach (var passwordAndRule in InputLines)
            {
                if (!Regex.IsMatch(passwordAndRule, DayTwoConstants.FormatRegex))
                {
                    continue;
                }

                var passPieces = passwordAndRule.Split(": ");
                var rule       = new PasswordRuleTwo(passPieces[0]);
                var password   = passPieces[1];

                if (!rule.IsValid())
                {
                    continue;
                }

                if (PasswordIsValidRuleTwo(password, rule))
                {
                    validPasswordCount++;
                }
            }

            return(validPasswordCount);
        }
Beispiel #2
0
 private bool PasswordIsValidRuleTwo(string password, PasswordRuleTwo rule)
 {
     if (password.Length < rule.SecondPosition)
     {
         return(false);
     }
     return(password[rule.FirstPosition - 1].Equals(rule.Letter) ^ password[rule.SecondPosition - 1].Equals(rule.Letter));
 }