-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSafeHacker2
executable file
·57 lines (45 loc) · 1.16 KB
/
SafeHacker2
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import random
import sys
import os
from datetime import datetime
# Convience variables
if len(sys.argv)>=3:
StartNumber=int(sys.argv[1])
EndNumber=int(sys.argv[2])
else:
print("Need a starting and ending number")
sys.exit(0)
# Get combination
# if combination file exists, read it
if os.path.exists('combination.txt'):
FileHandle=open('combination.txt','r')
combination=int(FileHandle.readline())
FileHandle.close()
else:
combination=random.randint(StartNumber,EndNumber)
FileHandle=open('combination.txt','w')
FileHandle.write(f"{combination}\n")
FileHandle.close()
# Build trial list
number=StartNumber
NumberList=[]
while number<EndNumber+1:
NumberList.append(number)
number+=1
# Try to guess combination
counter=0
startTime=datetime.now()
done=False
while not done:
trial=random.randint(0,len(NumberList)-1)
guess=NumberList[trial]
if guess==combination:
done=True
else:
NumberList.pop(trial)
counter+=1
endTime=datetime.now()
elapsedTime=endTime-startTime
print(f"Combination found: {guess}. It took {counter} tries in {elapsedTime}.")