Program.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace D11._1
  5. {
  6. class Program
  7. {
  8. static string Increment(string input)
  9. {
  10. if (input.Length == 0) return input;
  11. StringBuilder sb = new StringBuilder();
  12. var pos = input.Length - 1;
  13. var nchar = input[pos] + 1;
  14. if (nchar == 'i' || nchar == 'l' || nchar == 'o')
  15. nchar++;
  16. if (nchar > 'z')
  17. {
  18. nchar = 'a';
  19. var inc = Increment(input.Substring(0, pos));
  20. sb.Append(inc);
  21. sb.Append((char) nchar);
  22. }
  23. else
  24. {
  25. sb.Append(input.Substring(0, pos));
  26. sb.Append((char) nchar);
  27. }
  28. return sb.ToString();
  29. }
  30. static bool IsValid(string pwd)
  31. {
  32. bool hasIncreasing = false;
  33. bool hasForbiddenLettres = false;
  34. int repetitions = 0;
  35. bool repetitionExists = false;
  36. char last = '\0', lalast = '\0';
  37. int same = 1;
  38. foreach (var c in pwd)
  39. {
  40. if (c == 'i' || c == 'l' || c == 'o')
  41. {
  42. hasForbiddenLettres = true;
  43. break;
  44. }
  45. if (c == last) same++;
  46. else same = 1;
  47. if (same == 4) repetitionExists = true;
  48. if (repetitionExists == false && same == 2)
  49. {
  50. repetitions++;
  51. repetitionExists = repetitions >= 2;
  52. }
  53. if (lalast + 1 == last && last + 1 == c)
  54. hasIncreasing = true;
  55. lalast = last;
  56. last = c;
  57. }
  58. return hasForbiddenLettres == false && hasIncreasing && repetitionExists;
  59. }
  60. static void Main(string[] args)
  61. {
  62. if (args.Length < 1) throw new ArgumentException();
  63. string input = args[0];
  64. input = NexPassword(input);
  65. Console.WriteLine($"The next password is : {input}");
  66. input = NexPassword(input);
  67. Console.WriteLine($"The next password is : {input}");
  68. }
  69. private static string NexPassword(string input)
  70. {
  71. bool isValid = false;
  72. do
  73. {
  74. input = Increment(input);
  75. isValid = IsValid(input);
  76. } while (isValid == false);
  77. return input;
  78. }
  79. }
  80. }