wordle/game/words/words.go

67 lines
1.4 KiB
Go

package words
import (
_ "embed"
"strings"
"tildegit.org/jakew/wordle/game/util"
"time"
)
//go:embed words.txt
var words string
//go:embed dictionary.txt
var dictionary string
func GetSolution() (string, int) {
wordSlice := strings.Split(words, "\n")
today := time.Now().In(time.UTC).Truncate(24 * time.Hour)
day := (today.UnixMilli() - 1624057200000) / 864e5
index := day % int64(len(wordSlice))
return strings.ToLower(strings.TrimSpace(wordSlice[index])), int(day)
}
func contains(words string, word string) bool {
slice := strings.Split(words, "\n")
for _, item := range slice {
if item != "" && item == word {
return true
}
}
return false
}
func IsValid(word string) bool {
word = strings.TrimSpace(strings.ToLower(word))
if len(word) != 5 {
return false
}
return contains(words, word) || contains(dictionary, word)
}
func Check(word string, solution string) (correct, present []int) {
correct = []int{}
present = []int{}
exclude := []int{}
word = strings.TrimSpace(strings.ToLower(word))
for i, letter := range word {
// check if correct position
if letter == int32(solution[i]) {
correct = append(correct, i)
continue
}
// is the letter in the word
for j, letter1 := range solution {
if letter == letter1 && !util.ContainsInt(exclude, j) {
present = append(present, i)
exclude = append(exclude, j)
break
}
}
}
return
}