-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvent_of_code_5.py
More file actions
66 lines (52 loc) · 1.56 KB
/
advent_of_code_5.py
File metadata and controls
66 lines (52 loc) · 1.56 KB
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
62
63
64
65
66
import hashlib
ZEROES_COUNT = 5
PASSWORD_LENGTH = 8
def hash(string, number):
m = hashlib.md5()
m.update(string + str(number))
return m.hexdigest()
def is_correct_hash(hashstr):
return hashstr.startswith("".join([str(0) for _ in range(ZEROES_COUNT)]))
def find_password(door_id):
password = []
current_no = 0
while len(password) < PASSWORD_LENGTH:
h = hash(door_id, current_no)
if is_correct_hash(h):
print(current_no)
print(h)
password.append(h[5])
print(password)
current_no += 1
return "".join(password)
def test():
assert is_correct_hash(hash("abc", 3231929))
assert find_password("abc") == "18f47a30"
test()
print(find_password("wtnhxymk"))
def is_filled(password):
for letter in password:
if letter is None:
return False
return True
def find_better_password(door_id):
password = [None for _ in range(PASSWORD_LENGTH)]
current_no = 0
while not is_filled(password):
h = hash(door_id, current_no)
if is_correct_hash(h):
print(current_no)
print(h)
try:
index = int(h[5])
if 0 <= index < len(password) and password[index] is None:
password[index] = h[6]
print(password)
except ValueError:
pass
current_no += 1
return "".join(password)
def test_better():
assert find_better_password("abc") == "05ace8e3"
test_better()
print(find_better_password("wtnhxymk"))