Advent of Code 2015 day 11 part 1&2: solved

This commit is contained in:
Fabian Tessmer 2025-03-24 10:25:32 +01:00
parent c80cfc5c87
commit d6d81fc95c
3 changed files with 83 additions and 0 deletions

1
2015/day11/input.txt Normal file
View File

@ -0,0 +1 @@
vzbxkghb

29
2015/day11/problem.txt Normal file
View File

@ -0,0 +1,29 @@
--- Day 11: Corporate Policy ---
Santa's previous password expired, and he needs help choosing a new one.
To help him remember his new password after the old one expires, Santa has devised a method of coming up with a password based on the previous one. Corporate policy dictates that passwords must be exactly eight lowercase letters (for security reasons), so he finds his new password by incrementing his old password string repeatedly until it is valid.
Incrementing is just like counting with numbers: xx, xy, xz, ya, yb, and so on. Increase the rightmost letter one step; if it was z, it wraps around to a, and repeat with the next letter to the left until one doesn't wrap around.
Unfortunately for Santa, a new Security-Elf recently started, and he has imposed some additional password requirements:
Passwords must include one increasing straight of at least three letters, like abc, bcd, cde, and so on, up to xyz. They cannot skip letters; abd doesn't count.
Passwords may not contain the letters i, o, or l, as these letters can be mistaken for other characters and are therefore confusing.
Passwords must contain at least two different, non-overlapping pairs of letters, like aa, bb, or zz.
For example:
hijklmmn meets the first requirement (because it contains the straight hij) but fails the second requirement requirement (because it contains i and l).
abbceffg meets the third requirement (because it repeats bb and ff) but fails the first requirement.
abbcegjk fails the third requirement, because it only has one double letter (bb).
The next password after abcdefgh is abcdffaa.
The next password after ghijklmn is ghjaabcc, because you eventually skip all the passwords that start with ghi..., since i is not allowed.
Given Santa's current password (your puzzle input), what should his next password be?
Your puzzle answer was vzbxxyzz.
--- Part Two ---
Santa's password expired again. What's the next one?
Your puzzle answer was vzcaabcc.
Both parts of this puzzle are complete! They provide two gold stars: **

53
2015/day11/solution.py Normal file
View File

@ -0,0 +1,53 @@
def verify_password(password: str) -> bool:
if 'i' in password or 'o' in password or 'l' in password:
return False
last_char, pairs, previously_consecutive, contains_consecutive, previously_pair = '', 0, False, False, False
for letter in password:
if last_char == chr(ord(letter) - 1):
if previously_consecutive:
contains_consecutive = True
else:
previously_consecutive = True
else:
previously_consecutive = False
if last_char == letter and not previously_pair:
previously_pair = True
pairs += 1
else:
previously_pair = False
last_char = letter
return contains_consecutive and pairs > 1
def get_next_password(password: str) -> str:
def advance_char(char: chr, index: int) -> None:
if char == 'z' and index >= 0:
new_password[index] = 'a'
advance_char(new_password[index - 1], index - 1)
elif char != '' and index >= 0:
new_password[index] = chr(ord(char) + 1)
else:
pass
new_password = [i for i in password]
advance_char(new_password[-1], len(new_password) - 1)
while not verify_password(''.join(new_password)):
advance_char(new_password[-1], len(new_password) - 1)
return ''.join(new_password)
if __name__ == '__main__':
assert verify_password("abcdffaa"), "Error: Example 1 failed"
assert verify_password("ghjaabcc"), "Error: Example 2 failed"
assert not verify_password("ghjacbcc"), "Error: Example 3 failed"
assert get_next_password("abcdefgh") == "abcdffaa", "Error: Example 4 failed"
assert get_next_password("ghijklmn") == "ghjaabcc", "Error: Example 5 failed"
print("All test passed")
puzzle_input = open("input.txt", "r").readline()
next_password = get_next_password(puzzle_input)
print("solution: ", next_password)
print("Part2: ")
print("solution: ", get_next_password(next_password))