-
Notifications
You must be signed in to change notification settings - Fork 1
/
wordle.go
78 lines (70 loc) · 1.31 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
package main
// SPDX-License-Identifier: WTFPL
import (
"bufio"
"os"
"strings"
)
var validWords = map[string]struct{}{}
const (
Right rune = '🟩'
Known = '🟨'
Unknown = '⬜'
)
func checkGuessValid(guess string) bool {
_, ok := validWords[strings.ToLower(guess)]
return ok
}
func wordle(guess, inword string) string {
if len(guess) != len(inword) {
return ""
}
word := []rune(inword)
out := []rune(strings.Repeat(string(Unknown), len(word)))
seen := []rune(strings.Repeat(string(Unknown), len(word)))
for i, r := range guess {
if word[i] == r {
out[i] = Right
seen[i] = Right
}
}
for i, r := range guess {
if out[i] != Right {
wordPos := 0
for {
if f := strings.IndexRune(inword[wordPos:], r); f != -1 {
f = f + wordPos
if seen[f] == Unknown {
seen[f] = Known
out[i] = Known
break
}
if f < len(word)-1 {
wordPos = f + 1
} else {
break
}
} else {
break
}
}
}
}
return string(out)
}
func dictLoad(length int) {
words, err := os.Open("/usr/share/dict/words")
if err != nil {
panic(err)
}
defer words.Close()
scanner := bufio.NewScanner(words)
for scanner.Scan() {
w := scanner.Text()
if len(w) == length {
if strings.ToLower(w) == w {
validWords[w] = struct{}{}
}
}
}
}