-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
189 lines (166 loc) · 3.83 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"sync"
)
func FindSymmetricPoems() [][]string {
path := "./5.txt"
xss := ReadList(path)
dict := CreateDictionary(xss)
results := ParallelSearch(dict)
return RunesToStrings(results)
}
// ReadList
func ReadList(path string) []string {
AbsolutePath := func(_path interface{}) (interface{}, error) {
var path string = _path.(string)
return filepath.Abs(path)
}
OpenFile := func(_path interface{}) (interface{}, error) {
var path = _path.(string)
return os.OpenFile(path, os.O_RDONLY, os.ModePerm)
}
Scan := func(_file interface{}) (interface{}, error) {
var file *os.File = _file.(*os.File)
var result = []string{}
sc := bufio.NewScanner(file)
for sc.Scan() {
result = append(result, sc.Text())
}
defer file.Close()
err := sc.Err()
if err != nil {
fmt.Println(err)
return []string{}, err
} else {
return result, nil
}
}
_ReadList := func(path string) []string {
list, err := Bind(AbsolutePath, OpenFile, Scan)(path)
if err != nil {
fmt.Println(err)
return []string{}
} else {
return list.([]string)
}
}
return _ReadList(path)
}
func Bind(fs ...func(interface{}) (interface{}, error)) func(interface{}) (interface{}, error) {
return func(x interface{}) (interface{}, error) {
data := x
var err error
for _, f := range fs {
data, err = f(data)
if err != nil {
return nil, err
}
}
return data, nil
}
}
// ParallelSearch
func ParallelSearch(dict map[rune][][]rune) [][][]rune {
var results = [][][]rune{}
var resultsPtr interface{} = &results
ParallelForEach(Search(dict), dict, resultsPtr)
return results
}
func ParallelForEach(worker func(interface{}, interface{}, interface{}), dict map[rune][][]rune, results interface{}) {
var wg sync.WaitGroup
wg.Add(len(dict))
f := func(key interface{}, value interface{}) {
defer wg.Done()
worker(key, value, results)
}
for key, value := range dict {
go f(key, value)
}
wg.Wait()
}
// Search
func Search(dict map[rune][][]rune) func(key interface{}, _lines interface{}, _results interface{}) {
return func(key interface{}, _lines interface{}, _results interface{}) {
results := _results.(*[][][]rune)
lines := _lines.([][]rune)
for _, line := range lines {
var result [][]rune = [][]rune{line}
DFS(dict, result, results, 1)
}
}
}
func DFS(dict map[rune][][]rune, result [][]rune, results *[][][]rune, index int) {
if FoundQ(index) {
*results = append(*results, result)
PrintResult(result)
} else {
_DFS(dict, result, results, index)
}
}
func _DFS(dict map[rune][][]rune, result [][]rune, results *[][][]rune, index int) {
nextCharacter := result[0][index]
lines, ok := dict[nextCharacter]
if !ok {
return
}
for _, line := range lines {
if Validate(index, result, line) {
DFS(dict, append(result, line), results, index+1)
}
}
}
func FoundQ(index int) bool {
return index == 5
}
func PrintResult(result [][]rune) {
for _, x := range result {
fmt.Println(string(x))
}
fmt.Println("")
}
func Validate(index int, result [][]rune, rs []rune) bool {
for i := 1; i < index; i++ {
if result[i][index] != rs[i] {
return false
}
}
return true
}
// RunesToStrings
func RunesToStrings(rsss [][][]rune) [][]string {
var sss = make([][]string, len(rsss))
for index, rss := range rsss {
var ss = []string{}
for _, rs := range rss {
ss = append(ss, string(rs))
}
sss[index] = ss
}
return sss
}
// CreateDictionary
func CreateDictionary(xss []string) map[rune][][]rune {
var dict = map[rune][][]rune{}
for _, xs := range xss {
AppendLine(dict, xs)
}
return dict
}
func AppendLine(dict map[rune][][]rune, xs string) {
rs := []rune(xs)
key := rs[0]
rss, ok := dict[key]
if !ok {
dict[key] = [][]rune{rs}
} else {
dict[key] = append(rss, rs)
}
}
func main() {
var results = FindSymmetricPoems()
fmt.Println(results)
}