In [1]:
with open("input/day5.txt", "r") as f:
inputLines = [line for line in f]
doorId = bytes(inputLines[0].strip(), "utf-8")
In [2]:
import hashlib
import itertools
In [3]:
def interestingHashes(prefix):
for i in itertools.count():
m = hashlib.md5()
m.update(prefix + str(i).encode("utf-8"))
h = m.hexdigest()
if h.startswith("00000"):
yield h
In [4]:
def password1(prefix):
return "".join(h[5] for h in itertools.islice(interestingHashes(prefix), 8))
In [5]:
password1(doorId)
Out[5]:
In [6]:
def password2(prefix):
result = [None] * 8
for h in interestingHashes(prefix):
if h[5] in "01234567":
pos = int(h[5])
if result[pos] is None:
result[pos] = h[6]
if all(c is not None for c in result):
return "".join(result)
In [7]:
password2(doorId)
Out[7]: