AdventOfCode/2015/11/solution.py

85 lines
2.4 KiB
Python
Raw Permalink Normal View History

2024-12-09 14:16:56 +01:00
#!/bin/python3
2024-12-09 15:22:12 +01:00
import sys,time,re,string
2024-12-09 14:16:56 +01:00
from pprint import pprint
sys.path.insert(0, '../../')
from fred import list2int,get_re,nprint,lprint,loadFile
start_time = time.time()
2024-12-09 15:22:12 +01:00
input_f = 'input'
2024-12-09 14:16:56 +01:00
part = 1
#########################################
# #
# Part 1 #
# #
#########################################
if part == 1:
2024-12-09 15:22:12 +01:00
letters = string.ascii_lowercase
2024-12-09 14:16:56 +01:00
def findpairs(text):
pattern = r"([a-zA-Z])\1"
matches = []
i = 0
while i < len(text):
match = re.search(pattern, text[i:])
if match:
matches.append(match.group(0))
i += match.end(0)
else:
break
return matches
2024-12-09 15:22:12 +01:00
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
2024-12-09 14:16:56 +01:00
def validate(password:list):
for i in ['l','i','o']:
if i in password:
return False
2024-12-09 15:22:12 +01:00
if not find3chars(''.join(password)):
return False
2024-12-09 14:16:56 +01:00
if len(findpairs(''.join(password))) <= 1:
return False
2024-12-09 15:22:12 +01:00
return True
2024-12-09 14:16:56 +01:00
instructions = loadFile(input_f)
2024-12-09 15:22:12 +01:00
for inst in instructions:
print('Start',inst)
count = 0
current_word = list(inst)
while True:
new_word = "".join(current_word)
2024-12-09 14:16:56 +01:00
2024-12-09 15:22:12 +01:00
if validate(new_word):
print("Word found for Part",count+1,":",new_word)
count += 1
if count == 2:
break
2024-12-09 14:16:56 +01:00
2024-12-09 15:22:12 +01:00
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
2024-12-09 14:16:56 +01:00
#########################################
# #
# Part 2 #
# #
#########################################
if part == 2:
exit()
print("--- %s seconds ---" % (time.time() - start_time))