예제 #1
0
        private PasswordPolicy ParsePolicy(string policy)
        {
            var result       = new PasswordPolicy();
            var indexOfDash  = policy.IndexOf('-');
            var indexOfSpace = policy.IndexOf(' ');

            result.MinimalOccurrence = int.Parse(policy.Substring(0, indexOfDash));
            result.MaximumOccurrence = int.Parse(policy.Substring(indexOfDash + 1, indexOfSpace - indexOfDash - 1));
            result.Character         = char.Parse(policy.Substring(indexOfSpace + 1, 1));

            return(result);
        }
예제 #2
0
        public void AddNewPolicy(string pos1, string pos2, string letter, string password)
        {
            int minimum   = Int32.Parse(pos1);
            int maximum   = Int32.Parse(pos2);
            var newPolicy = new PasswordPolicy {
                FirstRule = minimum, SecondRule = maximum, Letter = Convert.ToChar(letter), Password = password
            };

            if (newPolicy.IsValidNewPolicy())
            {
                PasswordPolicies.Add(newPolicy);
            }
        }
예제 #3
0
        public int ValidatePasswords(IEnumerable <PasswordCheck> passwords, PasswordPolicy policy)
        {
            int validPasswords;

            if (policy == PasswordPolicy.OLD)
            {
                validPasswords = OldPolicyValidation(passwords);
            }
            else
            {
                validPasswords = NewPolicyValidation(passwords);
            }

            return(validPasswords);
        }
예제 #4
0
        public string Solve()
        {
            var inputs = Inputs.GetInputsValues("2");
            var lines  = inputs.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            var count  = 0;

            foreach (var line in lines)
            {
                if (PasswordPolicy.ValidateOne(PasswordPolicy.Parse(line)))
                {
                    count++;
                }
            }
            return(count.ToString());
        }
예제 #5
0
        static int Puzzle2(string[] input)
        {
            int validPasswords = 0;

            foreach (var line in input)
            {
                var parts = line.Split(':');

                PasswordPolicy policy = new PasswordPolicy(parts[0]);
                if (policy.ValidatePasswordPositions(parts[1].Trim()))
                {
                    validPasswords++;
                }
            }

            return(validPasswords);
        }
예제 #6
0
        /// <summary>
        /// The main method that is called outside this class that will solve the puzzle
        /// and return the answer
        /// </summary>
        /// <returns>The answer to the puzzle</returns>
        public int solvePuzzle()
        {
            int numTimesFoundAcceptedPassword = 0;

            // load all the puzzle data from the PuzzleData.txt file
            string puzzleData = LoadPuzzleDataIntoMemory();

            // split the puzzleData data into rows (find '\n' which indicates new line)
            string[] puzzleArray = puzzleData.Trim().Split('\n', StringSplitOptions.RemoveEmptyEntries);

            // go through each line
            foreach (string singlePuzzlePeace in puzzleArray)
            {
                PasswordPolicy passwordPolicy = new PasswordPolicy(singlePuzzlePeace);
                if (passwordPolicy.isPasswordValid_PuzzleOne())
                {
                    numTimesFoundAcceptedPassword++;
                }
            }

            return(numTimesFoundAcceptedPassword);
        }
예제 #7
0
        private static List <PasswordPolicy> ParseData(IList <string> lines)
        {
            // 1-8 n: dpwpmhknmnlglhjtrbpx

            List <PasswordPolicy> passwords = new List <PasswordPolicy>();

            foreach (string line in lines)
            {
                PasswordPolicy newPassword = new PasswordPolicy();
                string[]       split;

                split = line.Split('-');
                newPassword.minCount = int.Parse(split[0]);
                split = split[1].Split(' ');
                newPassword.maxCount        = int.Parse(split[0]);
                newPassword.charToMatch     = split[1][0];
                newPassword.passwordToCheck = split[2].TrimStart();

                passwords.Add(newPassword);
            }

            return(passwords);
        }
예제 #8
0
        private bool IsPasswordValid(PasswordPolicy policy, string password)
        {
            var occurrences = password.Split(policy.Character).Length - 1;

            return(occurrences >= policy.MinimalOccurrence && occurrences <= policy.MaximumOccurrence);
        }