-
Notifications
You must be signed in to change notification settings - Fork 0
/
best.go
232 lines (203 loc) · 4.7 KB
/
best.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
package main
import (
"fmt"
"runtime"
"sort"
"strings"
)
type Record struct {
Index int
Word string
Score int64
ListScore int
}
func findBest() {
inputs := make(chan Record, 64)
outputs := make(chan Record, 64)
go func() {
for i, word := range big {
inputs <- Record{
Index: i,
Word: word,
}
}
close(inputs)
}()
for i := 0; i < runtime.NumCPU(); i++ {
go func() {
counter := MakeCounter()
for record := range inputs {
for _, target := range small {
hints := calculateHints(target, record.Word)
count := counter.CountMatches(hints)
record.Score += count
}
outputs <- record
}
}()
}
records := make([]Record, len(big))
for i := range records {
record := <-outputs
records[record.Index] = record
fmt.Printf("\r%d/%d", i+1, len(big))
}
length := len(fmt.Sprint(len(big)))
fmt.Printf("\r%s\r", strings.Repeat(" ", 2*length+1))
sort.SliceStable(records, func(i, j int) bool {
return records[i].Score < records[j].Score
})
for i := 0; i < len(records) && i < 20; i++ {
record := records[i]
fmt.Printf("%s %.3f%%\n", record.Word, float64(100*record.Score)/float64(len(small)*len(small)))
}
}
func findBestFor(hints Hints) {
inputs := make(chan Record, 64)
outputs := make(chan Record, 64)
var list []string
for _, word := range small {
if matchesHints(word, hints) {
list = append(list, word)
}
}
if len(list) == 0 {
fmt.Println("no matches")
return
}
if len(list) == 1 {
fmt.Println(list[0])
return
}
fmt.Println(len(list))
go func() {
for i, word := range big {
inputs <- Record{
Index: i,
Word: word,
}
}
close(inputs)
}()
for i := 0; i < runtime.NumCPU(); i++ {
go func() {
counter := MakeCounter()
for record := range inputs {
for _, target := range list {
current := calculateHints(target, record.Word)
current = mergeHints(current, hints)
count := counter.CountMatches(current)
record.Score += count
}
outputs <- record
}
}()
}
records := make([]Record, len(big))
for i := range records {
record := <-outputs
records[record.Index] = record
fmt.Printf("\r%d/%d", i+1, len(big))
}
length := len(fmt.Sprint(len(big)))
fmt.Printf("\r%s\r", strings.Repeat(" ", 2*length+1))
listMap := make(map[string]bool)
for _, word := range list {
listMap[word] = true
}
smallMap := make(map[string]bool)
for _, word := range small {
smallMap[word] = true
}
for i, record := range records {
if listMap[record.Word] {
records[i].ListScore = 2
} else if smallMap[record.Word] {
records[i].ListScore = 1
}
}
sort.SliceStable(records, func(i, j int) bool {
if records[i].Score != records[j].Score {
return records[i].Score < records[j].Score
}
return records[i].ListScore > records[j].ListScore
})
for i := 0; i < len(records) && i < 20; i++ {
record := records[i]
fmt.Printf("%s %.3f%%\n", record.Word, float64(100*record.Score)/float64(len(list)*len(list)))
}
}
type Record2 struct {
Index int
WordA string
WordB string
Score int64
}
func findBest2() {
inputs := make(chan Record2, 64)
outputs := make(chan Record2, 64)
var recordCount int
for i := 0; i < len(small); i++ {
for j := i + 1; j < len(small); j++ {
recordCount++
}
}
go func() {
var index int
for i := 0; i < len(small); i++ {
for j := i + 1; j < len(small); j++ {
inputs <- Record2{
Index: index,
WordA: small[i],
WordB: small[j],
}
index++
}
}
close(inputs)
}()
for i := 0; i < runtime.NumCPU(); i++ {
go func() {
counter := MakeCounter()
last := ""
for record := range inputs {
if record.WordA != last {
last = record.WordA
counter.Reset()
}
for _, target := range small {
a := calculateHints(target, record.WordA)
b := calculateHints(target, record.WordB)
hints := mergeHints(a, b)
count := counter.CountMatches(hints)
record.Score += count
}
outputs <- record
}
}()
}
records := make([]Record2, recordCount)
for i := range records {
record := <-outputs
records[record.Index] = record
fmt.Printf("\r%d/%d", i+1, recordCount)
}
length := len(fmt.Sprint(recordCount))
fmt.Printf("\r%s\r", strings.Repeat(" ", 2*length+1))
best := make([]Record2, 20)
copy(best, records)
sort.SliceStable(best, func(i, j int) bool {
return best[i].Score < best[j].Score
})
for i := len(best); i < len(records); i++ {
if records[i].Score < best[len(best)-1].Score {
best[len(best)-1] = records[i]
sort.SliceStable(best, func(i, j int) bool {
return best[i].Score < best[j].Score
})
}
}
for _, record := range best {
fmt.Printf("%s,%s %.3f%%\n", record.WordA, record.WordB, float64(100*record.Score)/float64(len(small)*len(small)))
}
}