Program.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace D05._2
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. if (args.Length < 1) throw new ArgumentException();
  11. if (File.Exists(args[0]) == false) throw new FileNotFoundException();
  12. int goodStrings = 0;
  13. using (var file = File.OpenText(args[0]))
  14. {
  15. while (true)
  16. {
  17. var line = file.ReadLine();
  18. if (line == null) break;
  19. if (IsGoodString(line)) goodStrings++;
  20. }
  21. }
  22. Console.WriteLine($"The answer is : {goodStrings}");
  23. }
  24. private static bool IsGoodString(string line)
  25. {
  26. HashSet<(char a, char b)> repeted = new HashSet<(char a, char b)>();
  27. bool repetitionExists = false;
  28. bool betweenExists = false;
  29. int same = 1;
  30. char last = '\0', lalast = '\0';
  31. foreach (var c in line)
  32. {
  33. if (c == last) same++;
  34. else same = 1;
  35. if (same == 4) repetitionExists = true;
  36. if (repetitionExists == false && same != 3)
  37. {
  38. bool exists = repeted.Add((last, c));
  39. repetitionExists = exists == false;
  40. }
  41. if (lalast == c) betweenExists = true;
  42. if (betweenExists && repetitionExists) break;
  43. lalast = last;
  44. last = c;
  45. }
  46. return repetitionExists && betweenExists;
  47. }
  48. }
  49. }