DEV Community


Advent of Code 2020: Python Solution Day 1

Passionate ML and Game Dev learner from Nepal who loves sharing what he knows via blogging.
Updated on ・1 min read

This is my first ever challenge and I was amazed by the challenges. I am proficient in Python so all codes will be on Python3.

Day 1 seems to be easier to tackle.
I am saving my input as day1.txt on same directory as my Jupyter Notebook is.

with open("day1.txt", "r") as fp:
    num = fp.readlines()
# print(num)
num = [int(i.split("\n")[0]) for i in num]
print("Solution: 1\n")
# solution 1
for i in num:
    if 2020-i in num:
        print(i, 2020-i, i*(2020-i))

print("Solution: 2\n")        
# solution 2
for i in num:
    for j in num:
        if (2020-i - j) in num:
            print(2020-i-j, i, j)
Enter fullscreen mode Exit fullscreen mode

Result of above code is given below:

Solution: 1

933 1087 1014171
1087 933 1014171

Solution: 2

566 59 1395
1395 59 566
566 1395 59
59 1395 566
1395 566 59
59 566 1395
Enter fullscreen mode Exit fullscreen mode

I write blogs about Computer Vision projects on my GitHub page and if you got some time please share yours too.

Discussion (2)

paulmicheli profile image
Paul Micheli

Who's setting these challenges?

qviper profile image
Viper Author

Please follow

Forem Open with the Forem app