-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
372 lines (320 loc) · 9.9 KB
/
main.go
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"math/rand"
"os"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/archy-bold/wordle-go/game"
"github.com/archy-bold/wordle-go/strategy"
)
const (
NUM_LETTERS = 5
NUM_ATTEMPTS = 6
)
var ErrAllStartersFlagInvalid = errors.New("all-starters flag must be one of: valid, answers")
var ErrStrategyFlagInvalid = errors.New("strategy flag must be one of: minmax, charfreq")
var letters = []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
var startDate = time.Date(2021, time.June, 19, 0, 0, 0, 0, time.UTC)
var validWords = []string{}
var allAcceptedWords = []string{}
// var dictionary = map[string]int{}
type wordAnalysisResult struct {
word string
successes int
sumTries int
avgTries float64
longestGame int
}
func main() {
wordPtr := flag.String("word", "", "The game's answer")
cheatPtr := flag.Bool("cheat", false, "Whether to run the solver mode")
autoPtr := flag.Bool("auto", false, "Play the game automatically")
strategyPtr := flag.String("strategy", "charfreq", "Choose which strategy to use. One of 'minmax' (slow, effective) or 'charfreq' (fast, less effective).")
randomPtr := flag.Bool("random", false, "Choose a random word, if none specified. Otherwise gets daily word")
datePtr := flag.String("date", "", "If specified, will choose the word for this day")
starterPtr := flag.String("starter", "", "The starter word to use in strategies")
allPtr := flag.Bool("all", false, "Play all permutations of the answers")
allStartersPtr := flag.String("all-starters", "", "Play all permutations of the answers, with all permutations of the chosen starter list. Starter options are: valid (12972 iterations), answers (2315 iterations)")
flag.Parse()
if *allStartersPtr != "" && *allStartersPtr != "valid" && *allStartersPtr != "answers" {
fmt.Println(ErrAllStartersFlagInvalid.Error())
return
}
if *strategyPtr != "" && *strategyPtr != "minmax" && *strategyPtr != "charfreq" {
fmt.Println(ErrStrategyFlagInvalid.Error())
return
}
auto := *autoPtr
// Read the valid words
var err error
fmt.Print("Reading solutions... ")
err = readWordList(&validWords, "words/5/solutions.txt")
fmt.Printf("found %d\n", len(validWords))
check(err)
// Read the valid guesses
fmt.Print("Reading valid guesses... ")
err = readWordList(&allAcceptedWords, "words/5/guesses.txt")
// Sort the valid guesses as we will be searching that array often
sort.Strings(allAcceptedWords)
fmt.Printf("found %d\n", len(allAcceptedWords))
check(err)
reader := bufio.NewReader(os.Stdin)
var strat strategy.Strategy
if auto {
strat = getStrategy(*strategyPtr, *starterPtr)
}
// Play out all permutations
if *allPtr {
sumTries := 0
numSuccesses := 0
longestGame := 0
for i, answer := range validWords {
strat = getStrategy(*strategyPtr, *starterPtr)
g := game.CreateGame(answer, NUM_ATTEMPTS, &allAcceptedWords, i+1)
for {
word := strat.GetNextMove()
success, _ := g.Play(word)
strat.SetMoveOutcome(g.GetLastPlay())
if success {
score, of := g.GetScore()
fmt.Printf("%4d %s in %d/%d\n", i, answer, score, of)
numSuccesses++
sumTries += score
if longestGame < score {
longestGame = score
}
break
} else if g.HasEnded() {
fmt.Printf("%4d %s failed\n", i, answer)
break
}
}
}
fmt.Printf("Completed %d/%d\n", numSuccesses, len(validWords))
fmt.Printf("On average %f\n", float64(sumTries)/float64(numSuccesses))
fmt.Printf("Longest game %d/%d\n", longestGame, NUM_ATTEMPTS)
return
} else if *allStartersPtr != "" {
var results []wordAnalysisResult
var words []string
if *allStartersPtr == "answers" {
words = validWords
} else {
words = allAcceptedWords
}
results = make([]wordAnalysisResult, len(words))
for i, starter := range words {
var wg sync.WaitGroup
wg.Add(len(validWords))
fmt.Printf("%4d %s ", i, starter)
result := wordAnalysisResult{
word: starter,
successes: 0,
sumTries: 0,
avgTries: 0,
longestGame: 0,
}
var resMutex sync.Mutex
for _, answer := range validWords {
go func(result *wordAnalysisResult, resMutex *sync.Mutex, answer string) {
strat := strategy.NewCharFrequencyStrategy(NUM_LETTERS, letters, validWords, &allAcceptedWords, starter)
g := game.CreateGame(answer, 15, &allAcceptedWords, i+1)
for {
word := strat.GetNextMove()
success, _ := g.Play(word)
strat.SetMoveOutcome(g.GetLastPlay())
if success {
resMutex.Lock()
score, _ := g.GetScore()
result.sumTries += score
if score <= NUM_ATTEMPTS {
result.successes++
}
if result.longestGame < score {
result.longestGame = score
}
resMutex.Unlock()
wg.Done()
return
} else if g.HasEnded() {
wg.Done()
return
}
}
}(&result, &resMutex, answer)
}
wg.Wait()
result.avgTries = float64(result.sumTries) / float64(result.successes)
fmt.Printf("%4d %d %f\n", result.successes, result.longestGame, result.avgTries)
results[i] = result
}
// Sort by successes first
sort.Slice(results, func(i, j int) bool {
return results[i].successes > results[j].successes
})
fmt.Println("\nRanking: Num Successes")
for i := 0; i < 50; i++ {
res := results[i]
fmt.Printf("%d. %s %d %d %f\n", i+1, res.word, res.successes, res.longestGame, res.avgTries)
}
// Then average tries
sort.Slice(results, func(i, j int) bool {
return results[i].avgTries < results[j].avgTries
})
fmt.Println("\nRanking: Average Tries")
for i := 0; i < 50; i++ {
res := results[i]
fmt.Printf("%d. %s %d %d %f\n", i+1, res.word, res.successes, res.longestGame, res.avgTries)
}
// Finally the longest game
sort.Slice(results, func(i, j int) bool {
return results[i].longestGame > results[j].longestGame
})
fmt.Println("\nRanking: Longest Game")
fmt.Printf(" %s %d %d %f\n", results[0].word, results[0].successes, results[0].longestGame, results[0].avgTries)
return
}
// Cheat mode
if *cheatPtr {
strat = getStrategy(*strategyPtr, *starterPtr)
ug := game.CreateUnknownGame(NUM_LETTERS, NUM_ATTEMPTS)
for {
// Print the top 10 answers
fmt.Println("Top answers:")
suggestions := strat.GetSuggestions(10)
for i, suggestion := range suggestions {
if suggestion.Key != "" {
fmt.Printf(" %d: %s (%d)\n", i+1, suggestion.Key, suggestion.Value)
}
}
// Read the entered word from stdin
// TODO handle errors such as wrong sized word, wrong pattern for response
fmt.Print("Enter number of entered word, or word itself: ")
word, _ := reader.ReadString('\n')
word = strings.TrimSpace(word)
if idx, err := strconv.Atoi(word); err == nil && idx <= len(suggestions) {
word = suggestions[idx-1].Key
}
wordParts := strings.Split(word, "")
fmt.Print("Enter the result, where x is incorrect, o is wrong position, y is correct\n eg yxxox: ")
input, _ := reader.ReadString('\n')
parts := strings.Split(strings.TrimSpace((input)), "")
row := make([]game.GridCell, NUM_LETTERS)
for i, chr := range parts {
if chr == "x" {
row[i] = game.GridCell{
Letter: wordParts[i],
Status: game.STATUS_WRONG,
}
} else if chr == "y" {
row[i] = game.GridCell{
Letter: wordParts[i],
Status: game.STATUS_CORRECT,
}
} else if chr == "o" {
row[i] = game.GridCell{
Letter: wordParts[i],
Status: game.STATUS_INCORRECT,
}
}
}
// Update the game grid and strategy
strat.SetMoveOutcome(row)
complete, _ := ug.(*game.UnknownGame).AddResult(row)
if complete {
score, _ := ug.GetScore()
fmt.Printf("Hooray! (%d/%d)\n", score, NUM_ATTEMPTS)
return
}
}
}
// If no answer given in the word flag, choose
answer := *wordPtr
var gameNum int
if answer == "" {
if *randomPtr {
rand.Seed(time.Now().Unix())
gameNum = rand.Intn(len(validWords))
} else {
// Go by date
var dt time.Time
if *datePtr != "" {
dt, err = time.Parse("2006-01-02", *datePtr)
check(err)
// TODO check the date isn't before the start date or after the end date
} else {
dt = time.Now().UTC()
year, month, day := dt.Date()
dt = time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
}
gameNum = int(dt.Sub(startDate).Hours() / 24)
}
answer = validWords[gameNum]
}
g := game.CreateGame(answer, NUM_ATTEMPTS, &allAcceptedWords, gameNum)
for {
// Play based on whether a strategy is provided
var word string
if strat != nil {
word = strat.GetNextMove()
} else {
fmt.Print("Enter your guess: ")
input, _ := reader.ReadString('\n')
word = strings.TrimSpace(input)
}
// TODO handle error here
success, _ := g.Play(word)
if strat != nil {
strat.SetMoveOutcome(g.GetLastPlay())
}
if strat == nil || success || g.HasEnded() {
fmt.Println(g.OutputForConsole())
}
if success {
score, of := g.GetScore()
fmt.Println(g.OutputToShare())
fmt.Printf("Great work! %d/%d\n", score, of)
return
} else if g.HasEnded() {
fmt.Println(g.OutputToShare())
fmt.Printf("Better luck next time! The word was '%s'. X/%d\n", answer, NUM_ATTEMPTS)
return
}
}
}
func check(e error) {
if e != nil {
panic(e)
}
}
func getStrategy(strat string, starter string) strategy.Strategy {
if strat == "minmax" {
return strategy.NewMinMaxStrategy(NUM_LETTERS, validWords, &allAcceptedWords, starter)
}
return strategy.NewCharFrequencyStrategy(NUM_LETTERS, letters, validWords, &allAcceptedWords, starter)
}
func readWordList(arr *[]string, fname string) error {
data, err := Asset(fname)
if err != nil {
return err
}
scanner := bufio.NewScanner(strings.NewReader(string(data)))
i := 0
for scanner.Scan() {
word := scanner.Text()
// dictionary[word] = i
*arr = append(*arr, word)
i++
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}