-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwordlehack.go
More file actions
119 lines (112 loc) · 2.12 KB
/
Copy pathwordlehack.go
File metadata and controls
119 lines (112 loc) · 2.12 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
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
package main
import (
"encoding/json"
"fmt"
"os"
"regexp"
"runtime"
"strings"
"sync"
)
func maybefail(err error, msg string, args ...interface{}) {
if err == nil {
return
}
fmt.Fprintf(os.Stderr, msg, args...)
os.Exit(1)
}
type StringInt struct {
S string
I int
}
func guessThread(la []string, guesses chan string, out chan StringInt, wg *sync.WaitGroup) {
defer wg.Done()
var hasb [5]byte
var notb [5]byte
var posb [5]string
for guess := range guesses {
gb := []byte(guess)
count := 0
for _, target := range la {
if target == guess {
continue
}
has := hasb[:0]
not := notb[:0]
for gci, gc := range gb {
if gc == target[gci] {
posb[gci] = string(gb[gci : gci+1])
} else if strings.IndexByte(target, gc) != -1 {
has = append(has, gc)
posb[gci] = fmt.Sprintf("[^%c]", gc)
} else {
posb[gci] = "."
not = append(not, gc)
}
}
res := strings.Join(posb[:], "")
re := regexp.MustCompile(res)
for _, w := range la {
if !re.MatchString(w) {
continue
}
hit := true
for _, c := range has {
if strings.IndexByte(w, c) == -1 {
hit = false
break
}
}
if !hit {
continue
}
for _, c := range not {
if strings.IndexByte(w, c) != -1 {
hit = false
break
}
}
if !hit {
continue
}
count++
}
}
out <- StringInt{guess, count}
}
}
func submitter(la []string, guesses chan string) {
for _, guess := range la {
guesses <- guess
}
close(guesses)
}
func main() {
path := "La.json"
if len(os.Args) > 1 {
path = os.Args[1]
}
fin, err := os.Open(path)
maybefail(err, "%s: %v", path, err)
dec := json.NewDecoder(fin)
var la []string
err = dec.Decode(&la)
maybefail(err, "%s: []string: %v", path, err)
wg := sync.WaitGroup{}
guesses := make(chan string, 20)
results := make(chan StringInt, 20)
for i := 0; i < runtime.NumCPU(); i++ {
go guessThread(la, guesses, results, &wg)
wg.Add(1)
}
go submitter(la, guesses)
go func() {
wg.Wait()
close(results)
}()
for xr := range results {
guess := xr.S
count := xr.I
fmt.Printf("%s\t%d\n", guess, count)
}
}