| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- namespace D1._2
- {
- class Program
- {
- static void Main(string[] args)
- {
- if (args.Length < 1) return;
- if (File.Exists(args[0]) == false) return;
- var frequencies = new Dictionary<int, bool>()
- {
- { 0, true }
- };
- int frequency = 0;
- bool foundTwice = false;
- do
- {
- var file = File.OpenText(args[0]);
- foundTwice = ApplySequence(frequencies, ref frequency, ref foundTwice, file);
- file.Close();
- } while (foundTwice == false);
- if (foundTwice) Console.WriteLine($"Result : {frequency}");
- }
- private static bool ApplySequence(Dictionary<int, bool> frequencies, ref int frequency, ref bool foundTwice, StreamReader file)
- {
- do
- {
- var line = file.ReadLine();
- if (line == null) break;
- frequency += int.Parse(line);
- if (frequencies.ContainsKey(frequency)) foundTwice = true;
- else frequencies.Add(frequency, true);
- } while (foundTwice == false);
- return foundTwice;
- }
- }
- }
|