DEV Community

Viper
Viper

Posted on • Updated on

Advent of Code 2020: Python Solution Day 2

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.

Top comments (2)

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

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