-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpart_two.py
38 lines (27 loc) · 941 Bytes
/
part_two.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
from hashlib import md5
from typing import override
from infrastructure.solutions.base import Solution
class Year2015Day4Part2Solution(Solution):
HASH_PREFIX = '000000'
@classmethod
@override
def parse_input(cls, text_input: str) -> dict[str, str]:
return {'secret_key': text_input}
@classmethod
@override
def solve(cls, secret_key: str) -> int:
"""
Time: O((n+m)*16^m)
Space: O(n+m)
Where n - length of secret key
m - length of pattern (5 in this case)
"""
min_number = 0
while not cls.get_hash(secret_key, min_number).startswith(cls.HASH_PREFIX):
min_number += 1
return min_number
@staticmethod
def get_hash(secret_key: str, min_number: int) -> str:
return md5((secret_key + str(min_number)).encode()).hexdigest()
if __name__ == '__main__':
print(Year2015Day4Part2Solution.main())