DEV Community

loading...

Advent of Code 2020: Python Solution Day 2

qviper profile image Viper Updated on ・1 min read

This challenge can be found at this link.

My solution is:

with open("day2.txt", "r") as f:
    inp=f.readlines()

linp = [i.split("\n")[0] for i in inp]
# print(linp)
valid_2 = []
valid_1 = []
for lin in linp:
    key, value = lin.split(": ")
    char = key.split(" ")[1]
    # print(char, key.split(" ")[0].split("-"))
    n1, n2 = key.split(" ")[0].split("-")
    n1, n2 = int(n1)-1, int(n2)-1

    if n1+1<=value.count(char)<=n2+1:
        valid_1.append(lin)

    c = 0
    try:

        if value[int(n1)] == char:
            c+=1
        if value[int(n2)] == char:
            c+=1
        if c == 1:
            valid_2.append(lin)
    except:
        pass
print(f" Solution 1: {len(valid_1)}\nSolution 2:{len(valid_2)}")
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.

Discussion (2)

pic
Editor guide
Collapse
chaos0815 profile image
Christian Wolf

Having solved this challenge myself, I have a hard time reading your code because of lack of meaningfull varible names.

Collapse
qviper profile image
Viper Author

Yeah. Will try to make it more readable. Thanks.