Program.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace D1._2
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. if (args.Length < 1) return;
  11. if (File.Exists(args[0]) == false) return;
  12. var frequencies = new Dictionary<int, bool>()
  13. {
  14. { 0, true }
  15. };
  16. int frequency = 0;
  17. bool foundTwice = false;
  18. do
  19. {
  20. var file = File.OpenText(args[0]);
  21. foundTwice = ApplySequence(frequencies, ref frequency, ref foundTwice, file);
  22. file.Close();
  23. } while (foundTwice == false);
  24. if (foundTwice) Console.WriteLine($"Result : {frequency}");
  25. }
  26. private static bool ApplySequence(Dictionary<int, bool> frequencies, ref int frequency, ref bool foundTwice, StreamReader file)
  27. {
  28. do
  29. {
  30. var line = file.ReadLine();
  31. if (line == null) break;
  32. frequency += int.Parse(line);
  33. if (frequencies.ContainsKey(frequency)) foundTwice = true;
  34. else frequencies.Add(frequency, true);
  35. } while (foundTwice == false);
  36. return foundTwice;
  37. }
  38. }
  39. }