Move 2015 out of the way.

This commit is contained in:
Bert Peters
2016-12-01 11:25:19 +01:00
parent a75d14ec17
commit b37fd44fa7
50 changed files with 0 additions and 0 deletions

63
2015/day-11/solution.py Normal file
View File

@@ -0,0 +1,63 @@
from __future__ import print_function
import string
letters = [c for c in string.ascii_lowercase]
password = "hxbxwxba"
forbidden = set([letters.index(c) for c in "iol"])
passwordCode = [letters.index(c) for c in password]
def convert(password):
return [letters.index(c) for c in password]
def isOk(password):
hasStreak = False
prev = -2
streak = 1
same = 0
sameChar = None
for x in password:
if x in forbidden:
return False
if x == prev + 1:
streak += 1
if streak >= 3:
hasStreak = True
else:
streak = 1
if x == prev and x is not sameChar:
same += 1
sameChar = x
prev = x
return hasStreak and same >= 2
def incrementForbidden(password):
for idx, x in enumerate(password):
if x in forbidden:
password[idx] = (x + 1) % len(letters)
for i in range(idx + 1, len(password)):
password[i] = 0
return
def increment(password):
carry = True
i = len(password)
while carry and i > 0:
i -= 1
password[i] = (password[i] + 1) % len(letters)
carry = password[i] == 0
incrementForbidden(password)
for x in range(2):
increment(passwordCode)
while not isOk(passwordCode):
increment(passwordCode)
print("Next password is", ''.join([letters[c] for c in passwordCode]))