Advent Of Code-Day 1

Joby Ingram-Dodd
2 min readDec 3, 2020
Photo by Joshua Aragon on Unsplash

I recently joined Strive School on their AI engineer course and as part of our exercises I was introduced to the advent of code. Turns out it’s been running for a few years, although I’ve never heard of it before. Still it’s a nce way to improve your coding and what better way to procrastinate when you have course work to do.

Each day you need to solve 2 problems to collect your stars, if you are in the first 100 poeple to solve it you get points to move up the leaderboard. Day 1 started ok, nothing to crazy, but here is my take on the code.

Part 1

The part 1 task was to work through a list of numbers to find the 2 numbers which add up to 2020. Apparently the elves need them for their accounts, apparently they don’t have quickbooks.

The list of numbers supplied comes as a txt file so i started by bringing it in as multi-line string. Oh incidentally as we are learning AI on the course it’s pretty much all python coding so I’m doing all the challenges in python rather then my preferred swift.

So here is the simple code for part 1, I start by splitting up the string into lines, and loop through the lines. I decided the best approach was to loop through the numbers deducting each from 2020 then seeing if the result appeared in the list. Then print the 2 numbers, to submit the result we just multiply them together.

a = input.split(“\n”)
for x in a:
y = 2020 — int(x)

if str(y) in a:
print (x)
print (y)

Part 2

Part 2 simply adds in some complexity by asking you to find the 3 numbers that up to 2020. I stuck with the same process as before just adding another loop to the process. I added a check with the i variable to make sure I didn’t use the same number twice in my calculations. Then it was just a case of looping through the numbers twice and checking if they add up to 2020, finally printing the result of multiplying them.

for x in a: 
i = a.index(x)
for y in a:
iy = a.index(y)
if i != iy:
z = 2020 - int(x) - int(y)
if str(z) in a:
print( int(x) * int(y) * z)

Day 2 coming tomorrow, they get harder each day so lets hope I make it to day 25.

--

--