-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.py
42 lines (30 loc) · 942 Bytes
/
day4.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
42
# vi: set shiftwidth=4 tabstop=4 expandtab:
import datetime
import os
import itertools
import hashlib
top_dir = os.path.dirname(os.path.abspath(__file__)) + "/../../"
def get_key_from_file(file_path=top_dir + "resources/year2015_day4_input.txt"):
with open(file_path) as f:
for l in f:
return l.strip()
def get_coin(key, nb_zeros=5):
zeros = "0" * nb_zeros
for i in itertools.count():
s = key + str(i)
h = hashlib.md5(s.encode("utf-8")).hexdigest()
if h.startswith(zeros):
return i
def run_tests():
assert get_coin("abcdef") == 609043
assert get_coin("pqrstuv") == 1048970
def get_solutions():
key = get_key_from_file()
print(get_coin(key) == 282749)
print(get_coin(key, 6) == 9962624)
if __name__ == "__main__":
begin = datetime.datetime.now()
run_tests()
get_solutions()
end = datetime.datetime.now()
print(end - begin)