Solve 2015 days 11-14.

This commit is contained in:
2024-01-31 21:20:45 -05:00
parent 0dfb4f974a
commit 35a01f1f65
5 changed files with 169 additions and 1 deletions

49
2015/d11.py Normal file
View File

@@ -0,0 +1,49 @@
data = open(0).read().strip()
def is_valid(pw):
# 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.
ords = list(map(ord, pw))
for i in range(len(ords) - 2):
if ords[i] + 1 == ords[i + 1] and ords[i + 1] + 1 == ords[i + 2]:
break
else:
return False
# Passwords may not contain the letters i, o, or l, as these letters can be
# mistaken for other characters and are therefore confusing.
for c in "iol":
if c in pw:
return False
# Passwords must contain at least two different, non-overlapping pairs of letters, like aa, bb, or zz
pairs = set()
for i in range(len(pw) - 1):
if pw[i] == pw[i + 1]:
pairs.add(pw[i])
if not len(pairs) >= 2:
return False
return True
def inc(pw):
pw = list(map(ord, pw))
for i in range(len(pw) - 1, -1, -1):
pw[i] += 1
if pw[i] == ord('z') + 1:
pw[i] = ord('a')
else:
break
return "".join(map(chr, pw))
part_1 = False
valid_count = 0
while True:
data = inc(data)
if is_valid(data):
valid_count += 1
if part_1 and valid_count == 1:
break
elif valid_count == 2:
break
print(data)