## DEV Community

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:

linp = [i.split("\n") for i in inp]
# print(linp)
valid_2 = []
valid_1 = []
for lin in linp:
key, value = lin.split(": ")
char = key.split(" ")
# print(char, key.split(" ").split("-"))
n1, n2 = key.split(" ").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)}")
``````

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. Christian Wolf

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

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

DEV Community