Day two is a little more complicated than Day 1 was. Today’s challenge is to take a blob of passwords, and verify if they are acceptable or not, per the “company standard” at the time of the creation of each password.

For example:

1-3 a: abcde
1-3 b: cdefg
2-9 c: ccccccccc

For part 1, on line 1, the letter a should appear at least 1 time and at most 3 times. For line 2, the letter b should appear at least 1 time and at most 3 times. For line 3, the letter c should appear at least 2 times and at most 9 times.

My solution for Day 2, Part 1 is below:

number_valid = 0
toolow = 0
toohigh = 0
number_of_entries = 0

with open('day2data.txt') as f:
    lines = [line.rstrip() for line in f]

for x in lines:
  dashloc = x.find("-")
  spaceloc = x.find(" ")
  colloc = x.find(":")
  mincount=int(x[0:dashloc])
  maxcount=int(x[int(dashloc)+1:spaceloc])
  limitchar=x[int(spaceloc+1):int(colloc)]
  password = x[int(colloc+2):]
  checkvalue = int(password.count(limitchar))
  if(mincount <= checkvalue <= maxcount):
    number_valid=number_valid+1
  if(checkvalue<mincount):
    toolow+=1
  if(checkvalue>maxcount):
    toohigh+=1
  if(mincount>=maxcount):
    print "Problem?"

print number_valid
#print toolow
#print toohigh
#print number_valid+toolow+toohigh

On a note, I’ve got some debugging bits still in there, that are commented out. After reading the file in, I’ve initialized some variables I’ll be using in the code. Then I start looping through the data.

This is where my janky code really gets to shine, as I am sure there is a better way to handle this. First step is to locate the special characters separating the data, from the exampe, “1-3 a:abcde”, these are a dash, a space and a colon. With these located, I’ve extracted the min and max values, the letter I’m tracking, and the password itself.

After getting these core values, it’s simple enough to count the occurrences of the letter, then verify it against the min/max values and count how many are valid.

I had a bit of trouble at first because I had used “password = x[int(colloc+2):-1]” instead of “password = x[int(colloc+2):]”. I’m not a master of a lot of programming languages, but I am familiar with enough of them that I get the syntax confused a lot (I keep forgetting the colon on ifs and loops in Python). I forget which language uses -1 for “go to tot he end of a range”, but I am pretty sure there is one, because I did this a lot at first.

Part 2 changes things up a bit. Instead of the first numbers representing a range of how many, it demands that the special letter appears at one of those two positions, but not both.

My solution for part 2 is below:

number_valid = 0
number_of_entries = 0

with open('day2data.txt') as f:
    lines = [line.rstrip() for line in f]

for x in lines:
  first=0
  second=0
  dashloc = x.find("-")
  spaceloc = x.find(" ")
  colloc = x.find(":")
  mincount=int(x[0:dashloc])-1
  maxcount=int(x[int(dashloc)+1:spaceloc])-1
  limitchar=x[int(spaceloc+1):int(colloc)]
  password = x[int(colloc+2):]
  if(password[mincount]==limitchar):
    first=1
  if(password[maxcount]==limitchar):
    second=1
  if ((first==1) or (second==1)):
    if(first !=second):
      number_valid+=1

print number_valid
#print toolow
#print toohigh
#print number_valid+toolow+toohigh

More sloppy code, i didn’t even change the variable names. Now, instead of counting and comparing, I’m checking for the special character in each position, and toggling a variable if it’s there or not, then making sure it’s nor present in both. This is where the new variables “first” and “second” come in.

This biggest challenge on Day 2 was that it required more manipulation of the input data.

Github Repository of my Solutions