| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace D11._1
- {
- class Program
- {
- static string Increment(string input)
- {
- if (input.Length == 0) return input;
- StringBuilder sb = new StringBuilder();
- var pos = input.Length - 1;
- var nchar = input[pos] + 1;
- if (nchar == 'i' || nchar == 'l' || nchar == 'o')
- nchar++;
- if (nchar > 'z')
- {
- nchar = 'a';
- var inc = Increment(input.Substring(0, pos));
- sb.Append(inc);
- sb.Append((char) nchar);
- }
- else
- {
- sb.Append(input.Substring(0, pos));
- sb.Append((char) nchar);
- }
- return sb.ToString();
- }
- static bool IsValid(string pwd)
- {
- bool hasIncreasing = false;
- bool hasForbiddenLettres = false;
- int repetitions = 0;
- bool repetitionExists = false;
- char last = '\0', lalast = '\0';
- int same = 1;
- foreach (var c in pwd)
- {
- if (c == 'i' || c == 'l' || c == 'o')
- {
- hasForbiddenLettres = true;
- break;
- }
- if (c == last) same++;
- else same = 1;
- if (same == 4) repetitionExists = true;
- if (repetitionExists == false && same == 2)
- {
- repetitions++;
- repetitionExists = repetitions >= 2;
- }
- if (lalast + 1 == last && last + 1 == c)
- hasIncreasing = true;
- lalast = last;
- last = c;
- }
- return hasForbiddenLettres == false && hasIncreasing && repetitionExists;
- }
- static void Main(string[] args)
- {
- if (args.Length < 1) throw new ArgumentException();
- string input = args[0];
- input = NexPassword(input);
- Console.WriteLine($"The next password is : {input}");
- input = NexPassword(input);
- Console.WriteLine($"The next password is : {input}");
- }
- private static string NexPassword(string input)
- {
- bool isValid = false;
- do
- {
- input = Increment(input);
- isValid = IsValid(input);
- } while (isValid == false);
- return input;
- }
- }
- }
|