-
Notifications
You must be signed in to change notification settings - Fork 0
/
stupid_implementation.py
79 lines (57 loc) · 2.26 KB
/
stupid_implementation.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# -*- coding: utf-8 -*-
"""
Created on Thu May 24 20:25:44 2018
"""
__author__ = "Andrew I McClement"
from random import randint
from time import sleep
from evaluator import get_letter_from_integer, PasswordChecker
from evaluator import get_random_letter
from evaluator import MIN_PASSWORD_LENGTH, MAX_PASSWORD_LENGTH
class StupidSolver:
def __init__(self, checker: PasswordChecker):
self.checker = checker
self.guess = None
def solve(self) -> int:
self.guess = "0"*5
correct_count = 0
correct = False
for i in range(100):
correct_count, correct = self.checker.check_guess(self.guess)
if correct_count == len(self.guess) and not correct:
self.guess += "0"
if correct:
return self.checker.attempts
self.guess = self._improve_guess(self.guess, i, correct_count)
raise AssertionError("Did not solve password.")
def _improve_guess(self, guess: str, index, initial_correct) -> str:
for i in range(26):
guess = (guess[:index] + get_letter_from_integer(i)
+ guess[index + 1:])
current_correct, _ = self.checker.check_guess(guess)
if current_correct == initial_correct + 1:
return guess
raise AssertionError(f"Character at index {index} is not a letter.")
class ReallyStupidSolver:
def solve(self, checker: PasswordChecker) -> str:
while True:
length = randint(MIN_PASSWORD_LENGTH, MAX_PASSWORD_LENGTH)
guess = "".join((get_random_letter() for _ in range(length)))
_, success = checker.check_guess(guess)
if success:
return guess
def main():
puzzles = [PasswordChecker() for _ in range(10)]
stupid_solvers = [StupidSolver(checker) for checker in puzzles]
stupid_solutions = [(solver.solve(), solver.guess)
for solver in stupid_solvers]
print(stupid_solutions)
for checker in puzzles:
checker.reset()
solver = ReallyStupidSolver()
terrible_solutions = [(solver.solve(checker), checker.attemps)
for checker in puzzles]
print(terrible_solutions)
if __name__ == "__main__":
main()
sleep(5)