-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulate.py
70 lines (58 loc) · 1.96 KB
/
simulate.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
from collections import Counter
from guess import *
from pprint import pprint
import pandas as pd
ANSWERS_COUNT = 100
ITERATIONS = 100
def getHints(answer, guess):
matches = [i for i in range(0, len(answer)) if answer[i] == guess[i]]
mismatch_answer = [
answer[i] for i in range(0, len(answer)) if answer[i] != guess[i]
]
mismatch_guess = [guess[i] for i in range(0, len(answer)) if answer[i] != guess[i]]
mismatch_answer_d = dict(Counter(mismatch_answer))
hint = ""
for i in range(0, len(answer)):
if i in matches:
hint += "G"
else:
this_guess = mismatch_guess.pop(0)
if this_guess in mismatch_answer:
if mismatch_answer_d[this_guess] > 0:
hint += "P"
mismatch_answer_d[this_guess] -= 1
else:
hint += "B"
else:
hint += "B"
return hint
def runForAnswer(answer, exprs, n=ITERATIONS, length=LENGTH):
result = []
first_guess_choices = [x for x in exprs if len(set(list(x))) == len(x)]
first_guesses = random.sample(first_guess_choices, n)
for i in range(0, n):
done = False
iter_count = 0
curr_guess = first_guesses.pop()
while not done:
iter_count += 1
hints = getHints(answer, curr_guess)
result.append(
{
"run_idx": i,
"answer": answer,
"guess": curr_guess,
"hints": hints,
"iter": iter_count,
"num_poss": len(exprs),
}
)
if hints == "G" * LENGTH:
done = True
break
curr_guess, exprs = getNextGuess(curr_guess, hints, exprs)
return result
if __name__ == "__main__":
result = runForAnswer("3*42=126", getExprs())
df = pd.DataFrame.from_dict(result)
pdb.set_trace()