Solved 2015/11 P1+P2

This commit is contained in:
FrederikBaerentsen 2024-12-09 15:22:12 +01:00
parent c049c52d46
commit 5b4728200f
3 changed files with 53 additions and 20 deletions

View File

@ -43,7 +43,21 @@ For example:
Given Santa\'s current password (your puzzle input), what should his Given Santa\'s current password (your puzzle input), what should his
*next password* be? *next password* be?
Your puzzle input is `hxbxwxba`{.puzzle-input}. Your puzzle answer was `hxbxxyzz`.
Answer: ## \-\-- Part Two \-\-- {#part2}
Santa\'s password [expired
again]{title="The corporate policy says your password expires after 12 seconds. For security."}.
What\'s the next one?
Your puzzle answer was `hxcaabcc`.
Both parts of this puzzle are complete! They provide two gold stars:
\*\*
At this point, you should [return to your Advent calendar](/2015) and
try another puzzle.
Your puzzle input was `hxbxwxba`{.puzzle-input}.

1
2015/11/input2 Normal file
View File

@ -0,0 +1 @@
hxbxxyzz

View File

@ -1,11 +1,11 @@
#!/bin/python3 #!/bin/python3
import sys,time,re import sys,time,re,string
from pprint import pprint from pprint import pprint
sys.path.insert(0, '../../') sys.path.insert(0, '../../')
from fred import list2int,get_re,nprint,lprint,loadFile from fred import list2int,get_re,nprint,lprint,loadFile
start_time = time.time() start_time = time.time()
input_f = 'test' input_f = 'input'
part = 1 part = 1
######################################### #########################################
@ -15,7 +15,7 @@ part = 1
######################################### #########################################
if part == 1: if part == 1:
letters = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] letters = string.ascii_lowercase
def findpairs(text): def findpairs(text):
pattern = r"([a-zA-Z])\1" pattern = r"([a-zA-Z])\1"
@ -31,29 +31,47 @@ if part == 1:
break break
return matches return matches
def find3chars(text):
pattern = r"abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop|opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy|xyz"
matches = re.findall(pattern, text)
#print(matches)
if len(matches) > 0:
return True
else:
return False
def validate(password:list): def validate(password:list):
valid = False
for i in ['l','i','o']: for i in ['l','i','o']:
if i in password: if i in password:
return False return False
for i in range(0,len(password)-2): if not find3chars(''.join(password)):
idx = letters.index(password[i]) return False
if password[i+1] == letters[idx+1] and password[i+2] == letters[idx+2]:
valid = True
#print(password[i],password[i+1],password[i+2])
if len(findpairs(''.join(password))) <= 1: if len(findpairs(''.join(password))) <= 1:
return False return False
return valid return True
instructions = loadFile(input_f) instructions = loadFile(input_f)
for i in instructions: for inst in instructions:
i = list(i) print('Start',inst)
print(i) count = 0
if validate(i): current_word = list(inst)
while True:
input() new_word = "".join(current_word)
#print(instructions)
if validate(new_word):
print("Word found for Part",count+1,":",new_word)
count += 1
if count == 2:
break
for i in range(len(current_word) - 1, -1, -1):
current_letter_index = letters.index(current_word[i])
if current_letter_index < len(letters) - 1:
current_word[i] = letters[current_letter_index + 1]
current_word[i + 1:] = 'a' * (len(current_word) - i - 1)
break
######################################### #########################################