Advent of Code — Day 2

Joby Ingram-Dodd
3 min readDec 4, 2020
Photo by Joshua Reddekopp on Unsplash

DAY 1

Welcome to day 2 of the Advent of Code challenge, the level stepped up a little today but still pretty doable. I am still continuing on my python mission and even know some of the syntax now. As it happened my daughter woke me at 5:30 am which was not ideal but did give me some time to work through this issue.

Part 1

So the challange was to decipher some passwords and see if they meet the password rules set by North Pole Toboggan Rental Shop. As was the case in day 1 we were given a txt file with lines of text as below.

7-14 r: zrqmcfrvsrfrrvmr

The 2 numbers at the front show the lowest and highest number of times, the letter before the :, can appear in the password, our task was to work through the list and count the valid passwords. So here is the code I used to do it.

split = data.split("\n")
valid = 0
notValid = 0
#rules, the password must contain at least the letter at least the minimum number times and no more then the max
#step 1 is to make some functions that work for a single line in the input
def splitLine(x):
#takes a line and splits by space, then removes the : so we end up with the 2 numbers the letter and password as 3 variables
xs = x.split(" ")
y = xs[0]
l = xs[1].replace(":", "")
p = xs[2]
#pass these 3 variables to the next function
return getRule(y,l, p)

def getRule(y,l, p):
#this function breaks out the minium and mximum times the letter should appear
z = y.split("-")
minC = z[0]
maxC = z[1]
letter = l
#pass the min max , letter and password to the next function
return checkPwd(l, minC, maxC, p)


def checkPwd(l, mc, ma, p):
#this checks to see if the passwrod meets the rules
count = p.count(l)
#is the letter in the. password more then or equal to the min and less then or equal to the max
if count >= int(mc) and count <= int(ma):
return True
else:
return False
#returns all back through functions
#now run a for loop passing each line through the functions and updating the valid or invalid totals
for x in split:
if splitLine(x):
valid += 1
else:
notValid += 1

print(valid)

I like to split problems into their parts and use a function for each part, as it helps me make it clear in my mind. This is exactly what I did here.

  1. Function 1 splits the line up and passes the parts to the next function
  2. Function 2 splits out the rules the password should meet
  3. Function 3 checks the password against the rules

Finally I use a for loop to run through the list and update a counter for the valid passwords.

Part 2

As is the trend each day the second part adds complexity to the first task. Day 2 was no different, we now had to check the position of the letter inside the password. Using the example from above

7-14 r: zrqmcfrvsrfrrvmr

the letter ‘r’ can only be in possition 7 or 14 but not both, the twist being we must conpensate for 0 indexing and shift the index of the first letter to be 1. My approach was essentially the same as above but with the added checks included

#second partvalid2 = 0
notValid2 = 0
def splitLinetwo(x):
xs = x.split(" ")
y = xs[0]
l = xs[1].replace(":", "")
p = xs[2]
return getRule2(y,l, p)

def getRule2(y,l, p):
z = y.split("-")
minC = z[0]
maxC = z[1]
letter = l
return chkpwd(l,int(minC),int(maxC),p)
def chkpwd(l, mi, ma, p):
#print(p[mi])
if p[mi-1] == l and p[ma-1] != l:
return True
elif p[ma-1] == l and p[mi-1] != l:
return True
else:
return False

for x in split:
if splitLinetwo(x):
valid2 += 1
else:
notValid2 += 1


print(valid2)

Day 3 coming tomorow.

--

--