2016: d05: ex2: add solution
This commit is contained in:
parent
4188180886
commit
b4235a9b34
1 changed files with 34 additions and 0 deletions
34
2016/d05/ex2/ex2.py
Executable file
34
2016/d05/ex2/ex2.py
Executable file
|
|
@ -0,0 +1,34 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import hashlib
|
||||
import itertools
|
||||
import sys
|
||||
|
||||
|
||||
def solve(input: str) -> str:
|
||||
def crack_password(door_id: str) -> str:
|
||||
password = ["_"] * 8
|
||||
for i in itertools.count():
|
||||
hash = hashlib.md5((door_id + str(i)).encode()).hexdigest()
|
||||
if not hash.startswith("00000"):
|
||||
continue
|
||||
pos = hash[5]
|
||||
if pos not in ("0", "1", "2", "3", "4", "5", "6", "7"):
|
||||
continue
|
||||
if password[int(pos)] != "_":
|
||||
continue
|
||||
password[int(pos)] = hash[6]
|
||||
if all(c != "_" for c in password):
|
||||
break
|
||||
return "".join(password)
|
||||
|
||||
return crack_password(input.strip())
|
||||
|
||||
|
||||
def main() -> None:
|
||||
input = sys.stdin.read()
|
||||
print(solve(input))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue