Advertisement
Dojnaz

AoC Day1

Dec 17th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.01 KB | None | 0 0
  1. int result1 = 0;
  2. int result2 = 0;
  3. string[] inputArray = File.ReadAllLines(@"input.txt");
  4. for (int i = 0; i < inputArray.Length; i++)
  5. {
  6.     if (inputArray[i].Substring(0, 1) == "+")
  7.     {
  8.         result1 += Convert.ToInt32(inputArray[i].Remove(0, 1));
  9.     }
  10.     if (inputArray[i].Substring(0, 1) == "-")
  11.     {
  12.         result1 -= Convert.ToInt32(inputArray[i].Remove(0, 1));
  13.     }
  14. }
  15.  
  16. Console.WriteLine(result1);
  17. bool found = true;
  18.     List<int> freqs = new List<int>();
  19.     while (found)
  20.     {
  21.         for (int i = 0; i < inputArray.Length; i++) {
  22.             if (inputArray[i].Substring(0, 1) == "+") {
  23.                 result2 += Convert.ToInt32(inputArray[i].Remove(0, 1));
  24.             }
  25.             if (inputArray[i].Substring(0, 1) == "-") {
  26.                 result2 -= Convert.ToInt32(inputArray[i].Remove(0, 1));
  27.             }
  28.             if (freqs.Contains(result2)) { found = false; break; }
  29.             freqs.Add(result2);
  30.         }
  31.     }
  32.  
  33.     Console.WriteLine(result2);
  34.     File.Create("output.txt").Dispose();
  35.     using (TextWriter tw = new StreamWriter("output.txt")) {
  36.         tw.WriteLine(result1);
  37.         tw.WriteLine(result2);
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement