Files
aocpy/2015/d11.py
2024-10-20 15:19:25 -04:00

55 lines
1.4 KiB
Python

from lib import get_data
data = get_data(__file__).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 = True
valid_count = 0
while True:
data = inc(data)
if is_valid(data):
valid_count += 1
if part_1 and valid_count == 1:
print(data)
elif valid_count == 2:
print(data)
break