-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
90 lines (75 loc) · 1.76 KB
/
main.go
File metadata and controls
90 lines (75 loc) · 1.76 KB
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
package main
import (
"fmt"
"os"
"strings"
"time"
)
const (
wordsFileName = "english-words.txt"
quartilesFileName = "quartiles.txt"
)
func main() {
now := time.Now()
allWords, err := parseFile(wordsFileName)
if err != nil {
os.Exit(1)
}
quartiles, err := parseFile(quartilesFileName)
if err != nil {
os.Exit(1)
}
wordSet := make(map[string]struct{}, len(allWords))
for _, word := range allWords {
wordSet[word] = struct{}{}
}
guesses := generateGuesses(quartiles)
for _, guess := range guesses {
if _, found := wordSet[guess]; found {
fmt.Println("⚡️ found", guess)
}
}
fmt.Println("Time:", time.Since(now))
}
func parseFile(fileName string) ([]string, error) {
file, fileErr := os.Open(fileName)
if fileErr != nil {
fmt.Println("Error opening file:", fileErr)
return nil, fileErr
}
defer file.Close()
stats, statsErr := file.Stat()
if statsErr != nil {
fmt.Println("Error getting file stats:", statsErr)
return nil, statsErr
}
bytes := make([]byte, stats.Size())
_, readErr := file.Read(bytes)
if readErr != nil {
fmt.Println("Error reading file:", readErr)
return nil, readErr
}
rawText := string(bytes)
words := strings.Split(rawText, "\n")
for i := range words {
words[i] = strings.TrimSpace(words[i])
}
return words, nil
}
// Combine quartiles into all variations of 4-tile potential words
func generateGuesses(quartiles []string) []string {
possibleWords := []string{}
length := len(quartiles)
for a := range length {
for b := range length {
for c := range length {
for d := range length {
if a != b && a != c && a != d && b != c && b != d && c != d {
possibleWords = append(possibleWords, quartiles[a]+quartiles[b]+quartiles[c]+quartiles[d])
}
}
}
}
}
return possibleWords
}