-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordle.go
311 lines (253 loc) · 7.52 KB
/
wordle.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
package main
// playing a bit with recursion, then will move to concurrency
// NOPE, not working out as planned, nicer looking (?) than loops within loops.. but can't shrink candidate words, run-time is brutal
import (
"bufio"
"fmt"
"log"
"os"
"sort"
"strings"
)
// load words that have duplicat letters in them (true)? Or skip them cause they're not great starting words (false)?
const wantWordsWithDupeLetters bool = true
// if a letter appears more than once in a word, should that letter's score be counted once (false), or for every occurence (true)?
const scoreDupeLetters bool = false
// how many top scores to track? ie how many starting word combos to keep
const keepTopXScores int = 100
type kv struct {
Key string
Value int
}
var letters = make(map[string]int)
var allWords = make(map[string]int)
func initLetters() {
for letter := 'a'; letter <= 'z'; letter++ {
letters[string(letter)] = 0
}
}
func initWords(path string) {
file, err := os.Open(path)
if err != nil {
log.Fatalf("readLines: %s", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if !wantWordsWithDupeLetters && hasDupeLetters(scanner.Text()) {
// skip words that are low-value starting words..
continue
}
allWords[scanner.Text()] = 0
}
}
func scoreLetters() {
for letter := range letters {
for word := range allWords {
if strings.Contains(word, letter) {
letters[letter]++
}
}
}
}
func scoreWord(word string) int {
var tested string
var score int = 0
for _, letter := range word {
if string(letter) == "," {
continue
}
// only score each letter once
if !scoreDupeLetters && strings.ContainsRune(tested, letter) {
continue
}
tested = tested + string(letter)
score = score + letters[string(letter)]
}
return score
}
func scoreWords() {
for word := range allWords {
allWords[word] = scoreWord(word)
}
}
func sortScoredThings(scoredThings map[string]int) []kv {
var sortedScoredThings []kv
for k, v := range scoredThings {
sortedScoredThings = append(sortedScoredThings, kv{k, v})
}
sort.Slice(sortedScoredThings, func(i, j int) bool {
return sortedScoredThings[i].Value > sortedScoredThings[j].Value
})
return sortedScoredThings
}
func hasDupeLetters(word string) bool {
for _, letter := range word {
if letter == ',' {
continue
}
if strings.Count(word, string(letter)) > 1 {
// fmt.Printf("dupe in word %s, %c\n", word, letter)
return true
}
}
return false
}
func findStarters(startingCombos map[string]int, shrinkingWords map[string]int, depth int) map[string]int {
if depth <= 0 {
fmt.Println("depth 0, returning startingCombos")
for startingCombo := range startingCombos {
startingCombos[startingCombo] = scoreWord(startingCombo)
}
return startingCombos
}
// to hold the new longer combos as we iterate recursively
var newStartingCombos = make(map[string]int)
// first pass, populate with "words"
if len(startingCombos) == 0 {
fmt.Println("first pass, populating startingCombos")
for word := range shrinkingWords {
newStartingCombos[word] = 0
}
} else {
for startingCombo := range startingCombos {
for word := range shrinkingWords {
newStartingCombo := startingCombo + "," + word
if uniqueStartingCombo(newStartingCombo, newStartingCombos) {
newStartingCombos[newStartingCombo] = 0
}
}
}
}
return findStarters(newStartingCombos, shrinkingWords, depth-1)
}
func uniqueStartingCombo(newStartingCombo string, newStartingCombos map[string]int) bool {
var foundComboMatch bool
splitWords := strings.Split(newStartingCombo, ",")
for startingCombo := range newStartingCombos {
foundComboMatch = true // assume match until proven otherwise
for i := 0; i < len(splitWords); i++ {
if !strings.Contains(startingCombo, splitWords[i]) {
foundComboMatch = false
}
}
if foundComboMatch {
return false
}
}
return true
}
func buildStartingCombos() map[string]int {
// four layers deep
var startingCombos = make(map[string]int)
var wordTwos, wordThrees, wordFours map[string]int
//var wordOnes, wordTwos, wordThrees, wordFours map[string]int
var topX []int
var keep bool
wordTwos = allWords
for wordOne := range allWords {
wordTwos = removeWord(wordOne, wordTwos)
wordThrees = wordTwos
for wordTwo := range wordTwos {
wordThrees = removeWord(wordTwo, wordThrees)
wordFours = wordThrees
for wordThree := range wordThrees {
wordFours = removeWord(wordThree, wordFours)
for wordFour := range wordFours {
// TODO - make this loop run concurrently, performance is crap (can't pre-score the words with new scoring idea (scoring the combo, rather than summing the word scores))
startingCombo := wordOne + "," + wordTwo + "," + wordThree + "," + wordFour
// startingComboScore := allWords[wordOne] + allWords[wordTwo] + allWords[wordThree] + allWords[wordFour]
startingComboScore := scoreWord(startingCombo)
keep, topX = worthKeeping(startingComboScore, topX)
if keep {
startingCombos[startingCombo] = startingComboScore
}
}
}
}
}
return startingCombos
}
func worthKeeping(score int, topScores []int) (bool, []int) {
// we want to populate at least keepTopXScores worth of scores..
if len(topScores) <= keepTopXScores {
topScores = append(topScores, score)
return true, topScores
}
// sort topX scores
sort.Ints(topScores)
// walk sorted slice biggest to smallest value - is our score bigger?
for i := len(topScores) - 1; i == 0; i-- {
if score > topScores[i] {
topScores[i] = score
return true, topScores
}
}
return false, topScores
}
func pruneThing(scoredThing map[string]int) map[string]int {
sortedScoredThing := sortScoredThings(scoredThing)
// prune to..
topX := 10000
// need to be within slice bounds
currentLength := len(sortedScoredThing)
if topX > currentLength {
topX = currentLength
}
var prunedSortedScoredThing = make(map[string]int)
for _, kv := range sortedScoredThing[0:topX] {
prunedSortedScoredThing[kv.Key] = kv.Value
}
return prunedSortedScoredThing
}
func removeWord(dirtyWord string, words map[string]int) map[string]int {
// strips a word out of the slice..
var cleanWords = make(map[string]int)
for word := range words {
if word != dirtyWord {
cleanWords[word] = words[word]
}
}
return cleanWords
}
func printSortedScoredThing(sortedScoredThing []kv, topX int) {
if topX > len(sortedScoredThing) || topX == 0 {
topX = len(sortedScoredThing)
}
for _, kv := range sortedScoredThing[:topX] {
fmt.Printf("%s %d\n", kv.Key, kv.Value)
}
}
func main() {
initLetters()
initWords("c:\\temp\\wordlewords.txt.short")
scoreLetters()
var topX int
// from https://github.com/tabatkins/wordle-list
// what'd we read?
fmt.Printf("Read %d words", len(allWords))
// score and print letters
fmt.Println("--- scored letters ---")
sortedScoredLettes := sortScoredThings(letters)
printSortedScoredThing(sortedScoredLettes, 0)
fmt.Println("------")
/////////////// score words
scoreWords()
fmt.Printf("Scored %d words", len(allWords))
////////////// top X scored words
topX = 10
fmt.Printf("--- top %d scored words ---\n", topX)
// sort the scored words
sortedScoredWords := sortScoredThings(allWords)
printSortedScoredThing(sortedScoredWords, topX)
fmt.Println("------")
////////////// starting combos
topX = 10
fmt.Printf("--- top %d starting combos ---\n", topX)
// build our word combos
startingCombos := buildStartingCombos()
// sort the scored words
sortedStartingCombos := sortScoredThings(startingCombos)
printSortedScoredThing(sortedStartingCombos, 0)
fmt.Println("------")
}