-
Notifications
You must be signed in to change notification settings - Fork 0
/
day5.py
61 lines (45 loc) · 1.6 KB
/
day5.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# vi: set shiftwidth=4 tabstop=4 expandtab:
import datetime
import os
import hashlib
import itertools
top_dir = os.path.dirname(os.path.abspath(__file__)) + "/../../"
def get_door_id_from_file(file_path=top_dir + "resources/year2016_day5_input.txt"):
with open(file_path) as f:
for l in f:
return l.strip()
def yield_hashes(door_id, nb_zeros):
zeros = "0" * nb_zeros
for i in itertools.count():
val = door_id + str(i)
h = hashlib.md5(val.encode("utf-8")).hexdigest()
if h.startswith(zeros):
yield h[nb_zeros:]
def compute_password(door_id, length=8, nb_zeros=5):
return "".join(
h[0] for h in itertools.islice(yield_hashes(door_id, nb_zeros), length)
)
def compute_password2(door_id, length=8, nb_zeros=5):
password = [None] * length
for h in yield_hashes(door_id, nb_zeros):
position, value = h[0], h[1]
if "0" <= position <= "7":
position_int = int(position)
if password[position_int] is None:
password[position_int] = value
if None not in password:
return "".join(password)
def run_tests():
door_id = "abc"
assert compute_password(door_id) == "18f47a30"
assert compute_password2(door_id) == "05ace8e3"
def get_solutions():
door_id = get_door_id_from_file()
print(compute_password(door_id) == "c6697b55")
print(compute_password2(door_id) == "8c35d1ab")
if __name__ == "__main__":
begin = datetime.datetime.now()
run_tests()
get_solutions()
end = datetime.datetime.now()
print(end - begin)