Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var aoc = new AdventOfCode(2018, 1);
- //cleaner would be
- var answerA = aoc.InputLines.Select(x => int.Parse(x)).Sum();
- aoc.SubmitAnswer(answerA, Part.A);
- //convert each string to int
- var lines = aoc.InputLines.Select(x => int.Parse(x)).ToArray();
- //this is the accumelator
- int frequency = 0;
- //track values seen for part B
- HashSet<int> seen = new HashSet<int>();
- //how many cycles through the list,
- int cycle = 0;
- //keep running till we break
- while (true)
- {
- //test each item
- foreach (var element in lines)
- {
- //add
- frequency += element;
- //check if allready seen for PartB
- if (seen != null && seen.Contains(frequency))
- {
- seen = null; //set to null, so we can stop, not necessary in this case, but is cleaner.
- aoc.SubmitAnswer(frequency, Part.B);
- //done
- return;
- }
- //add to hashset
- if(seen != null) seen.Add(frequency);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment