public string SolvePartA(IEnumerable <string> input, int capacity, int preamble) { SumsChecker sc = new SumsChecker(capacity); IEnumerable <long> numbers = input .Select <string, long?>(line => long.TryParse(line, out long n) ? n : null) .Where(maybeInt => maybeInt.HasValue) .Select(maybeInt => maybeInt.Value); return(FindInvalidNumber(numbers, sc, preamble).ToString()); }
private long FindInvalidNumber(IEnumerable <long> numbers, SumsChecker checker, int preamble) { foreach (long n in numbers.Take(preamble)) { checker.AddNumber(n); } foreach (long n in numbers.Skip(preamble)) { if (!checker.Contains(n)) { return(n); } checker.AddNumber(n); } return(-1); }