-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.py
More file actions
111 lines (95 loc) · 2.82 KB
/
solver.py
File metadata and controls
111 lines (95 loc) · 2.82 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from multiprocessing import Pool
from pprint import pprint
from collections import Counter
import math
scored_words = eval(open("./scored_words.txt").read())
def best_guess(constraints):
best_word = None
viable_candidates = 0
for word in scored_words:
passes = all(constraint(word) for constraint in constraints)
if not passes:
continue
best_word = best_word or word
viable_candidates += 1
return best_word, viable_candidates
def not_contains(letter):
def apply(word):
return letter not in word
return apply
def not_at(letter, index):
def apply(word):
return word[index] != letter
return apply
def at(letter, index):
def apply(word):
return word[index] == letter
return apply
def contains(letter):
def apply(word):
return letter in word
return apply
def not_word(bad_word):
def apply(word):
return word != bad_word
return apply
def get_constraints(guess, verdict):
if verdict == "bad":
return [not_word(guess)]
constraints = []
for index, (letter, v) in enumerate(zip(guess, verdict)):
if v == 'B':
constraints.append(not_contains(letter))
if v == 'Y':
constraints.append(not_at(letter, index))
constraints.append(contains(letter))
if v == 'G':
constraints.append(at(letter, index))
return constraints
def get_verdict(guess, secret):
def verdict_char(gs):
g, s = gs
if g == s:
return "G"
elif g in secret:
return "Y"
else:
return "B"
return ''.join(map(verdict_char, zip(guess, secret)))
def solve(secret):
constraints = []
verdict = "BBBBB"
guesses = 0
while verdict != "GGGGG":
guess, viable_candidates = best_guess(constraints)
if secret is None:
print("Viable candidates:", viable_candidates)
print("Suggested guess:", guess)
verdict = input("Verdict: ")
else:
verdict = get_verdict(guess, secret)
constraints += get_constraints(guess, verdict)
guesses += 1
return guesses
def benchmark():
with Pool() as p:
n = len(scored_words)
solved = sorted(p.map(solve, scored_words))
avg = sum(solved) / n
histogram = Counter(solved)
return {
"n": n,
"average": avg,
"p50": solved[int(n * 0.5)],
"p95": solved[int(n * 0.95)],
"p99": solved[int(n * 0.99)],
"max": max(solved),
"min": min(solved),
"stddev": math.sqrt(sum((xi - avg) ** 2 for xi in solved) / n),
"histogram": histogram.items()
}
def main():
solve(secret = None)
if __name__ == "__main__":
main()
#pprint(benchmark())