| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- namespace D05._2
- {
- class Program
- {
- static void Main(string[] args)
- {
- if (args.Length < 1) throw new ArgumentException();
- if (File.Exists(args[0]) == false) throw new FileNotFoundException();
- int goodStrings = 0;
- using (var file = File.OpenText(args[0]))
- {
- while (true)
- {
- var line = file.ReadLine();
- if (line == null) break;
- if (IsGoodString(line)) goodStrings++;
- }
- }
- Console.WriteLine($"The answer is : {goodStrings}");
- }
- private static bool IsGoodString(string line)
- {
- HashSet<(char a, char b)> repeted = new HashSet<(char a, char b)>();
- bool repetitionExists = false;
- bool betweenExists = false;
- int same = 1;
- char last = '\0', lalast = '\0';
- foreach (var c in line)
- {
- if (c == last) same++;
- else same = 1;
- if (same == 4) repetitionExists = true;
- if (repetitionExists == false && same != 3)
- {
- bool exists = repeted.Add((last, c));
- repetitionExists = exists == false;
- }
- if (lalast == c) betweenExists = true;
- if (betweenExists && repetitionExists) break;
- lalast = last;
- last = c;
- }
- return repetitionExists && betweenExists;
- }
- }
- }
|