Write a method named checkSum252
(based on the previous exercise, checkSum25
) that not only finds a single corrupted number in a stream of integers, but also finds a contiguous range of two or more numbers that sums to that corrupted number and returns the sum of the min/max in that range.
Your method should run the same algorithm as before to find the single corrupted number c that cannot be made by summing any 2 of the previous 25 numbers.
Then you should find a contiguous range of integers in the file that add up to a sum of exactly c.
(You may assume that such a range exists somewhere in the file.)
Return the sum of the minimum and maximum integer value in this range.
For example, consider the following data stored in the file numbers.txt
:
35
20
15
25
47
40
62
55
65
95
102
117
150
182
127
219
299
277
309
576
In this list, adding up all of the numbers from 15 through 40 produces the invalid number from checkSum25
of 127.
The range of numbers above that adds to exactly 127 is: 15, 25, 47, 40.
Your job is to find the appropriate range in the input data,
then return the sum of the smallest and largest integer in that range.
For example, for the preceding example, the call of checkSum252("numbers.txt")
should return 15 + 47 or 62
.
You may assume that the file exists and is readable,
that it follows the format described above,
and that there will be a valid range that sums to exactly the given value.
(This exercise is based on the Advent of Code 2020, day 9.)