-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwordle.py
169 lines (131 loc) · 4.01 KB
/
wordle.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# wordle.py -- botfight harness for wordle
USAGE = '''\
This is a harness to write bots that play wordle.
See: https://www.powerlanguage.co.uk/wordle/
to play against the computer:
$ python wordle.py human
to test a bot named "play" in "my-bot.py" against the word "apple":
$ python wordle.py word apple my-bot.play
to test your bot against a dictionary:
$ python wordle.py wordlist wordlist.txt my-bot.play
to play 1000 games using randomly chosed words from a wordlist:
$ python wordle.py wordlist wordlist.txt my-bot.play 1000
'''
import sys, importlib, hashlib, random
MAGIC = 'WORDLE'
g_random = None
def get_random():
global g_random
if None == g_random:
g_random = random.Random(MAGIC)
return g_random
def load_wordlist(fn):
a = []
for i in open(fn).readlines():
a.append(i[:-1])
return a
def load_bot(s):
fn, func = s.split('.')
module = importlib.import_module(fn)
bot = getattr(module, func)
return bot
def get_play(bot, guess_num, secret_hash, last_guess, last_score):
state = '%d\t%s\t%s\t%s' % (guess_num, secret_hash, last_guess, last_score)
response = bot(state)
return response
def calc_score(secret, guess):
a = []
for i, ch in enumerate(secret):
g = '-'
if i < len(guess):
g = guess[i]
if g == ch:
a.append('3')
elif g in secret:
a.append('2')
else:
a.append('1')
return ''.join(a)
def play_word(bot, secret):
guess_num = 0
guess = '-' * len(secret)
score = calc_score(secret, guess)
secret_hash = hashlib.sha256((MAGIC + secret).encode()).hexdigest()[:7]
while 1:
guess = get_play(bot, guess_num, secret_hash, guess, score)
score = calc_score(secret, guess)
sys.stdout.write('PLAY\t%d\t%s\t%s\t%s\t%s\n' % (guess_num, secret_hash, secret, guess, score))
if guess == secret:
return guess_num
guess_num += 1
def play_wordlist(bot, wordlist, n):
total_guesses = 0
if 0 == n:
count = len(wordlist)
else:
count = n
for i in range(count):
if 0 != n:
word = get_random().choice(wordlist)
else:
word = wordlist[i]
guesses = play_word(bot, word)
total_guesses += guesses
i += 1
sys.stdout.write('WORD\t%d\t%d\t%d\t%f\t%s\n' % (i, guesses, total_guesses, total_guesses / float(i), word))
return total_guesses
def play_human(secret):
guess_num = 0
guess = '-' * len(secret)
while 1:
score = calc_score(secret, guess)
sys.stdout.write('guess_num: %d, last_guess: %s, last_score: %s\n' % (guess_num, guess, score))
sys.stdout.write('Your guess?\n')
guess = sys.stdin.readline().strip()
guess_num += 1
if guess == secret:
break
sys.stdout.write('Congratulations! You solved it in %d guesses.\n' % guess_num)
return guess_num
def main(argv):
if 0 == len(argv):
print(USAGE)
sys.exit()
c = argv[0]
if 0:
pass
elif 'human' == c:
if 2 == len(argv):
secret = argv[1]
else:
secret = get_random().choice(list(filter(lambda x: len(x) == 5 and len(set(list(x))) == 5, load_wordlist('sowpods.txt'))))
x = play_human(secret)
return x
elif 'help' == c:
print(USAGE)
sys.exit()
elif 'score' == c:
secret = argv[1]
guess = argv[2]
x = calc_score(secret, guess)
print(x)
elif 'word' == c:
secret = argv[1]
bot = load_bot(argv[2])
x = play_word(bot, secret)
return x
elif 'wordlist' == c:
fn_wordlist = argv[1]
bot = load_bot(argv[2])
n = 0
if 4 == len(argv):
n = int(argv[3])
wordlist = load_wordlist(fn_wordlist)
x = play_wordlist(bot, wordlist, n)
return x
else:
print(USAGE)
sys.exit()
if __name__ == '__main__':
x = main(sys.argv[1:])
sys.exit(x)