예제 #1
0
        public override object Part2(string input)
        {
            var numbers = input.ToLines()
                          .Select(long.Parse)
                          .OrderByDescending(x => x)
                          .ToArray();

            var parts = new long[3];

            if (!SumN.Find(numbers, 2020, ref parts))
            {
                throw new Exception("Panic! Unable to solve");
            }

            return(parts.Aggregate((x, y) => x * y));
        }
예제 #2
0
        private static long FindInvalidNumber(long[] numbers, Range search)
        {
            while (true)
            {
                var preamble = numbers[search]
                               .OrderByDescending(x => x)
                               .ToArray();

                var parts = new long[2];
                if (!SumN.Find(preamble, numbers[search.End], ref parts))
                {
                    return(numbers[search.End]);
                }

                search = (search.Start.Value + 1)..(search.End.Value + 1);
            }
        }