From 04577c4c8d18e0edb4ae35e9ea797fcc20ae716a Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Mon, 3 Dec 2018 14:20:08 +0100 Subject: [PATCH] Day01_Part2: Add code to pass tests --- AdventOfCode2018/Day01.cs | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/AdventOfCode2018/Day01.cs b/AdventOfCode2018/Day01.cs index 1474f6d..ad5f4a7 100644 --- a/AdventOfCode2018/Day01.cs +++ b/AdventOfCode2018/Day01.cs @@ -1,4 +1,4 @@ -using System; +using System.Collections.Generic; namespace AdventOfCode2018 { @@ -85,7 +85,35 @@ namespace AdventOfCode2018 public string ResolveDay01_Part2(string[] inputs) { - throw new NotImplementedException(); + int accumulator = 0; + List accumulatorHistory = new List(); + int? repeatedAccumulator = null; + while (repeatedAccumulator == null) + { + foreach (string input in inputs) + { + accumulatorHistory.Add(accumulator); + int intInput; + if (int.TryParse(input.Substring(1), out intInput)) + { + if (input[0] == '-') + { + accumulator -= intInput; + } + if (input[0] == '+') + { + accumulator += intInput; + } + + if (accumulatorHistory.Contains(accumulator)) + { + repeatedAccumulator = accumulator; + break; + } + } + } + } + return repeatedAccumulator.ToString(); } } }