Solution to problem 5 part 1 in Python
This commit is contained in:
52
src/Year_2016/P5.py
Normal file
52
src/Year_2016/P5.py
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
# --- Day 5: How About a Nice Game of Chess? ---
|
||||||
|
|
||||||
|
# You are faced with a security door designed by Easter Bunny engineers that
|
||||||
|
# seem to have acquired most of their security knowledge by watching hacking
|
||||||
|
# movies.
|
||||||
|
|
||||||
|
# The eight-character password for the door is generated one character at a
|
||||||
|
# time by finding the MD5 hash of some Door ID (your puzzle input) and an
|
||||||
|
# increasing integer index (starting with 0).
|
||||||
|
|
||||||
|
# A hash indicates the next character in the password if its hexadecimal
|
||||||
|
# representation starts with five zeroes. If it does, the sixth character in
|
||||||
|
# the hash is the next character of the password.
|
||||||
|
|
||||||
|
# For example, if the Door ID is abc:
|
||||||
|
|
||||||
|
# The first index which produces a hash that starts with five zeroes is
|
||||||
|
# 3231929, which we find by hashing abc3231929; the sixth character of the
|
||||||
|
# hash, and thus the first character of the password, is 1.
|
||||||
|
# 5017308 produces the next interesting hash, which starts with
|
||||||
|
# 000008f82..., so the second character of the password is 8.
|
||||||
|
# The third time a hash starts with five zeroes is for abc5278568,
|
||||||
|
# discovering the character f.
|
||||||
|
|
||||||
|
# In this example, after continuing this search a total of eight times, the
|
||||||
|
# password is 18f47a30.
|
||||||
|
|
||||||
|
# Given the actual Door ID, what is the password?
|
||||||
|
|
||||||
|
# Your puzzle input is abbhdwsy.
|
||||||
|
|
||||||
|
|
||||||
|
from hashlib import md5
|
||||||
|
|
||||||
|
_input = "abbhdwsy"
|
||||||
|
|
||||||
|
|
||||||
|
def part_1() -> None:
|
||||||
|
passwd = []
|
||||||
|
end = "9" * len(_input)
|
||||||
|
for num in range(int(end)):
|
||||||
|
testing = _input + str(num)
|
||||||
|
result = md5(testing.encode()).hexdigest()
|
||||||
|
if result.startswith("0" * 5):
|
||||||
|
passwd.append(result[5])
|
||||||
|
if len(passwd) == 8:
|
||||||
|
print(f"The password is {''.join(passwd)}")
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
part_1()
|
||||||
Reference in New Issue
Block a user