-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem79.py
38 lines (31 loc) · 1.02 KB
/
problem79.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
'''
By analysing a user's login attempts, can you determine the
secret numeric passcode?
'''
import itertools
def check_password(password, attempt):
'passwordがattemptに整合するかをチェック'
a, b, c = tuple(attempt)
lo = password.find(a)
hi = len(password) - password[::-1].find(c)
if lo < 0 or hi > len(password):
return False
for i in range(lo + 1, hi):
if password[i] == b:
return True
return False
def main():
with open('src/keylog.txt', 'r', encoding='utf-8') as f:
attempts = [row.rstrip() for row in f]
for length in itertools.count(8):
for tpl in itertools.permutations('01236789', length):
password = ''.join(tpl)
if all((check_password(password, attempt)
for attempt in attempts)):
return password
if __name__ == '__main__':
import time
t1 = time.time()
print(main())
t2 = time.time()
print('{:.3f} s'.format(t2 - t1))