Equd

AdventOfCode 2018 Day 01

Dec 1st, 2018
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.89 KB | None | 0 0
  1. var aoc = new AdventOfCode(2018, 1);
  2.  
  3. //cleaner would be
  4. var answerA = aoc.InputLines.Select(x => int.Parse(x)).Sum();
  5. aoc.SubmitAnswer(answerA, Part.A);
  6.  
  7.  
  8. //convert each string to int
  9. var lines = aoc.InputLines.Select(x => int.Parse(x)).ToArray();
  10.  
  11. //this is the accumelator
  12. int frequency = 0;
  13.  
  14. //track values seen for part B
  15. HashSet<int> seen = new HashSet<int>();
  16.  
  17. //how many cycles through the list,
  18. int cycle = 0;
  19.  
  20. //keep running till we break
  21. while (true)
  22. {
  23.     //test each item   
  24.     foreach (var element in lines)
  25.     {
  26.         //add
  27.         frequency += element;
  28.  
  29.         //check if allready seen for PartB
  30.         if (seen != null && seen.Contains(frequency))
  31.         {                  
  32.             seen = null; //set to null, so we can stop, not necessary in this case, but is cleaner.
  33.             aoc.SubmitAnswer(frequency, Part.B);           
  34.            
  35.             //done
  36.             return;
  37.         }
  38.         //add to hashset
  39.         if(seen != null) seen.Add(frequency);
  40.     }  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment