main About me Back to Posts Personal Projects Links Privacy Contact
I found out about a thing called, “The Advent of Code”. It’s like an advent calendar for software geeks, so, ahem… I love it!
Every day they publish two problems you get to solve via code, like puzzles essentially. You can use any language to solve the puzzles. You then sign in to the site and submit the answer your code came up with to prove you solved it. For each puzzle you complete, you get 1 “star” which you use to track your progress.
Today’s first puzzle has you get a list of numbers from them, find the two from that list which sum up to 2020 and then multiply them to get the final result.
The second puzzle just has you do the same thing but looking for three values from the list instead of two.
***Spoiler Alert ***
Here’s my solution to the first puzzle:
# Advent of Code #1 - Find the two entries that sum to 2020; what do you get if you multiply them together?
myExpenses = []
file = open("c:\\git\\advent-2020\\1.txt", "r")
lines = file.readlines()
file.close()
myExpenses = lines[0].split(',')
lastEntry = 0
found = False
for entry in myExpenses:
if found == True:
break
if entry == '':
continue
for entry2 in myExpenses:
if entry2 == '':
continue
if (int(entry) + int(entry2)) == 2020:
print("The two entries are : " + str(entry) + " and " + str(entry2))
print("When multiplied, the result is :" + str(int(entry) * int(entry2)))
found = True
break
if found == False:
print("Couldn't find any entries adding up to 2020.")
And here’s my solution to the second part (summing three results):
# Advent of Code #2 - Find the three entries that sum to 2020; what do you get if you multiply them together?
myExpenses = []
file = open("1.txt", "r")
lines = file.readlines()
file.close()
myExpenses = lines[0].split(',')
lastEntry = 0
found = False
for entry in myExpenses:
if found == True:
break
if entry == '':
continue
for entry2 in myExpenses:
if entry2 == '':
continue
for entry3 in myExpenses:
if entry3 == '':
continue
if (int(entry) + int(entry2) + int(entry3)) == 2020:
print("The three entries are : " + str(entry) + " and " + str(entry2) + " and " + str(entry3))
print("When multiplied, the result is :" + str(int(entry) * int(entry2) * int(entry3)))
found = True
break
if found == False:
print("Couldn't find any entries adding up to 2020.")