DEV Community

Viper
Viper

Posted on • Updated on

Advent of Code 2020: Python Solution Day 1

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 q-viper.github.io and if you got some time please share yours too.

Top comments (2)

Collapse
 
paulmicheli profile image
Paul Micheli

Who's setting these challenges?

Collapse
 
qviper profile image
Viper

Please follow adventofcode.com