示例#1
0
        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());
        }
示例#2
0
        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);
        }