So since we’ve had a little bit of free time lately, I’ve been doing some python scripting. The other day I decided to go back to Advent of Code and work on it a little bit more.
— Day 4: Secure Container —
You arrive at the Venus fuel depot only to discover it’s protected by a password. The Elves had written the password on a sticky note, but someone threw it out.
However, they do remember a few key facts about the password:
- It is a six-digit number.
- The value is within the range given in your puzzle input.
- Two adjacent digits are the same (like 22 in 122345).
- Going from left to right, the digits never decrease; they only ever increase or stay the same (like 111123 or 135679).
Other than the range rule, the following are true:
- 111111 meets these criteria (double 11, never decreases).
- 223450 does not meet these criteria (decreasing pair of digits 50).
- 123789 does not meet these criteria (no double).
Part 1
How many different passwords within the range given in your puzzle input meet these criteria?
So for me, my range was 124075 – 580769. This was a pretty straight forward problem. First, iterate through all numbers within the range. As you go through each number, check to see if it meets a few conditions.
total = 0 for n in range (124075, 580769): num = [int(i) for i in str(n)] adjacent = 0 decrease = 1 #Check for adjacent values for i in range (0, 5): if num[i] == num[i+1]: adjacent = 1 break #Check for decrease for i in range (0, 5): if num[i+1] < num[i]: decrease = 0 break if adjacent and decrease: total += 1 print(n) print(total)
Part 2
An Elf just remembered one more important detail: the two adjacent matching digits are not part of a larger group of matching digits.
Given this additional criterion, but still ignoring the range rule, the following are now true:
- 112233 meets these criteria because the digits never decrease and all repeated digits are exactly two digits long.
- 123444 no longer meets the criteria (the repeated 44 is part of a larger group of 444).
- 111122 meets the criteria (even though 1 is repeated more than twice, it still contains a double 22).
How many different passwords within the range given in your puzzle input meet all of the criteria?
Again, pretty straight forward. Really only had to make a minor change to my code to see if the adjacent numbers were exactly two, or not.
total = 0 for n in range (124075, 580769): num = [int(i) for i in str(n)] adjacent = 0 decrease = 1 #Check for adjacent values for i in range (0, 5): if num[i] == num[i+1]: if num.count(num[i]) == 2: adjacent = 1 #Check for decrease for i in range (0, 5): if num[i+1] < num[i]: decrease = 0 break if adjacent and decrease: total += 1 print(n) print(total)