-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvent4.py
41 lines (33 loc) · 1.33 KB
/
advent4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#! /usr/bin/env python
# --- Day 4: The Ideal Stocking Stuffer ---
#
# Santa needs help mining some AdventCoins (very similar to bitcoins) to use as gifts for all the economically
# forward-thinking little girls and boys.
#
# To do this, he needs to find MD5 hashes which, in hexadecimal, start with at least five zeroes. The input to the
# MD5 hash is some secret key (your puzzle input, given below) followed by a number in decimal. To mine AdventCoins,
# you must find Santa the lowest positive number (no leading zeroes: 1, 2, 3, ...) that produces such a hash.
#
# For example:
#
# - If your secret key is abcdef, the answer is 609043, because the MD5 hash of abcdef609043 starts with five zeroes
# (000001dbbfa...), and it is the lowest such number to do so.
#
# - If your secret key is pqrstuv, the lowest number it combines with to make an MD5 hash starting with five zeroes
# is 1048970; that is, the MD5 hash of pqrstuv1048970 looks like 000006136ef....
#
# --- Part Two ---
#
# Now find one that starts with six zeroes.
import hashlib
SECRET_KEY = "yzbqklnj"
def find_md5_with_prefix(prefix):
i = 0
while True:
i += 1
md5 = hashlib.md5("{}{}".format(SECRET_KEY, i)).hexdigest()
if md5.startswith(prefix):
return i
if __name__ == "__main__":
n = find_md5_with_prefix("000000")
print n