AdventOfCode/2015/11/solution.py

85 lines
2.4 KiB
Python

#!/bin/python3
import sys,time,re,string
from pprint import pprint
sys.path.insert(0, '../../')
from fred import list2int,get_re,nprint,lprint,loadFile
start_time = time.time()
input_f = 'input'
part = 1
#########################################
# #
# Part 1 #
# #
#########################################
if part == 1:
letters = string.ascii_lowercase
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
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):
for i in ['l','i','o']:
if i in password:
return False
if not find3chars(''.join(password)):
return False
if len(findpairs(''.join(password))) <= 1:
return False
return True
instructions = loadFile(input_f)
for inst in instructions:
print('Start',inst)
count = 0
current_word = list(inst)
while True:
new_word = "".join(current_word)
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
#########################################
# #
# Part 2 #
# #
#########################################
if part == 2:
exit()
print("--- %s seconds ---" % (time.time() - start_time))