-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
172 lines (135 loc) · 3.06 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
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"sync"
"time"
)
type Node struct {
subnodes map[string]Node
}
func (node Node) InsertWord(word string) {
firstChar := word[0:1]
foundNode, exists := node.subnodes[firstChar]
if !exists {
foundNode = Node{subnodes: make(map[string]Node)}
node.subnodes[firstChar] = foundNode
}
if len(word) > 1 {
// There is another character
foundNode.InsertWord(word[1:])
} else if len(word) == 1 {
// No more characters
foundNode.subnodes[""] = Node{subnodes: make(map[string]Node)}
}
}
func (node Node) FindWord(word string) bool {
firstChar := word[0:1]
foundNode, exists := node.subnodes[firstChar]
if exists {
if len(word) == 1 {
_, exists = foundNode.subnodes[""]
return exists
}
return foundNode.FindWord(word[1:])
}
return false
}
func join(ins []rune, c rune) (result []string) {
for i := 0; i <= len(ins); i++ {
result = append(result, string(ins[:i])+string(c)+string(ins[i:]))
}
return
}
func permutations(testStr string) []string {
var n func(testStr []rune, p []string) []string
n = func(testStr []rune, p []string) []string {
if len(testStr) == 0 {
return p
} else {
result := []string{}
for _, e := range p {
result = append(result, join([]rune(e), testStr[0])...)
}
return n(testStr[1:], result)
}
}
output := []rune(testStr)
perms := n(output[1:], []string{string(output[0])})
return unique(perms)
}
func worker(nodeWord Node, results chan<- string, jobs <-chan string, wg *sync.WaitGroup) {
defer wg.Done()
for lookupWord := range jobs {
if nodeWord.FindWord(lookupWord) {
results <- lookupWord
}
}
}
func unique(stringSlice []string) []string {
keys := make(map[string]bool)
list := []string{}
for _, entry := range stringSlice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
func loadWords() Node {
word_bytes, err := ioutil.ReadFile("words.txt")
if err != nil {
fmt.Println("Error while reading file", err)
os.Exit(1)
}
words := strings.Split(string(word_bytes), "\n")
root := Node{subnodes: make(map[string]Node)}
for _, word := range words {
word := strings.TrimSpace(word)
root.InsertWord(word)
}
return root
}
func loadLookupWord() string {
args := os.Args[1:]
if len(args) == 0 {
fmt.Println("No lookup word supplied")
os.Exit(1)
}
lookupWord := string(args[0])
return strings.TrimSpace(lookupWord)
}
func elapsed(what string) func() {
start := time.Now()
return func() {
fmt.Printf("%s took %v\n", what, time.Since(start))
}
}
func main() {
defer elapsed("page")()
lookupWord := loadLookupWord()
rootWord := loadWords()
wordPerm := permutations(lookupWord)
results := make(chan string)
jobs := make(chan string, len(wordPerm))
numberOfWorkers := 5
wg := &sync.WaitGroup{}
for i := 0; i < numberOfWorkers; i++ {
wg.Add(1)
go worker(rootWord, results, jobs, wg)
}
for _, perm := range wordPerm {
jobs <- perm
}
close(jobs)
go func() {
wg.Wait()
close(results)
}()
for res := range results {
fmt.Println(res)
}
}