Hello! We are on Day 09 of Advent of Code 2020.
Spoilers Ahead! The full solution to the problem is available here.
Problem - Part 01
Given a list of numbers, wherein the first n
numbers is called a preamble, and the numbers after the preamble have to be a sum of any of the numbers within the preamble. Find the first number (called invalid number) in the list of numbers that defies this rule.
To solve this we would have to do the following:
- Get all the numbers after the preamble.
- Initialize a
start
-> 0 andend
->n
(or) preamble length to keep track of the elements in the preamble. - Loop through all the numbers after the preamble, and check if the current number is possibly a sum of any two elements in the preamble from start -> end, if not return false using a function
has_sum(num, preamble
- The first number after the preamble that returns false for
has_sum
is our invalid number.
The has_sum
function that checks if a target sum is present when adding any of the two numbers in a given list, would look like:
The result of the find_number_that_disobeys_preamble
function is the invalid number ie. the answer to Part 01.
Problem - Part 02
For Part 02, we use the invalid number from Part 01, and we need to find if a contiguous sequence of numbers in the given number list, equal to the invalid number. Let’s call this contiguos sequence of numbers [a..b..]
. The sum of smallest of [a..b..
] and the largest of [a..b..]
is the answer to the Part 02 of this puzzle.
- To find the contiguous sum, we use a similar two pointer approach,
start
andend
, initialized to 0 and 1 referring to the zeroth and the first element in thenumbers
array. - We loop through and get the sum of all values in the numbers array between the indices
start
->end
. - If the sum equals our target sum ie. our invalid number, we break from the loop and returning a slice of the array of numbers from start to end in the numbers list.
- If the current sum is lesser our target, that means we need to expand the window ie. add more numbers so we increment the
end
pointer. - If the current sum is greater than our target, that means we need to shrink the window from the left.
Now, all that’s left to do is find the smallest and largest values in this contiguous subsequence, and add them.
That’s all for Day 09. Thanking you for taking time to read this blogpost, and see you tomorrow!