Advent of Code 2020 Day 1
So, I got downstairs to do my Day 1 Advent of Code submission around 5:15 AM CST (which is 6 hours, 15 minutes after it was made available). I checked twitter this morning to see @ericwastl (who by the way created/runs Advent of Code) tweeting out that the DB is resizing.
Looks like 2020 has struck yet another amazing service, but this time for the better. Hopefully more people are getting their fix of programming puzzles with this year's Advent of Code, I know I enjoy these puzzles and the idea behind them.
Now, onto my solution!
This first part of the first puzzle was to find the product of the two numbers from the input that summed to 2020. The easiest way to approach this is to brute-force the list of data in order to go through all possible combinations and when you find two numbers that sum up to 2020, return the product of those two values. This is exactly what I did to get this first part working. Below is the pseudocode for this type of operation.
for fisrt_number in number_list
for second_number in number_list(after first_number)
if first_number + second_number == 2020
return first_number * second_number
This is what I'd call the brute-force approach and it's not amazingly quick for LARGE inputs. Luckily, the input provided for this work was fairly small (only 200 lines) so this would be pretty easy to do. Other than reading from files (which I'm still pretty terrible at in Java) that's all that was needed.
Part 2 typically adds a twist on the part 1 puzzle, this twist can be adding more logic to the processing of the data, or in this case adding another number to add to the sum. Now, instead of summing two numbers up to 2020, three numbers must add up to 2020 and we return the product of those 3 numbers. Since my plan worked for part 1, I used it in part 2 by adding another inner for-loop and my work there was done.
for first_number in number_list
for second_number in number_list (after first_number)
for third_number in number_list (after second_number)
if first_number + second_number + third_number == 2020
return first_number * second_number * third_number
This is still a super brute-force approach and I think I should probably work on looking for alternative route logic to attack these kinds of problems (these kinds of problems show up continually on programming puzzles, so it would benefit me to do it).
My full solution is available on GitHub and now that I have the maven configuration all set up and ready, I just need to get some quick work set up, like reducing potential copy-paste code (CPD would help with this). I think I might add some static analysis tools for the future, but for now, I'll just blog about it 😄.
If you want to join my private leaderboard on Advent of Code, feel free to add yourself to the private leaderboard with code 699615-aae0e8af
. While today's submissions do not count for private leaderboards, future ones might.
Member discussion