-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
85 lines (73 loc) · 1.53 KB
/
filter.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
package wordle
type Filter interface {
Filter(string) bool
}
type runeFilter interface {
runeFilter([]rune) bool
}
type filters []runeFilter
func (ff filters) Filter(s string) bool {
runes := []rune(s)
for _, f := range ff {
if !f.runeFilter(runes) {
return false
}
}
return true
}
type filterFunc func([]rune) bool
var falseFilter = filterFunc(func([]rune) bool { return false })
func (ff filterFunc) runeFilter(runes []rune) bool {
return ff(runes)
}
func containsRune(runes []rune, r rune) bool {
for _, x := range runes {
if x == r {
return true
}
}
return false
}
func noSpotFilter(r rune) runeFilter {
return filterFunc(func(runes []rune) bool {
return !containsRune(runes, r)
})
}
func correctSpotFilter(r rune, x int) runeFilter {
if x < 0 {
return falseFilter
}
return filterFunc(func(runes []rune) bool {
return x < len(runes) && runes[x] == r
})
}
func wrongSpotFilter(r rune, x int, ignores []int) runeFilter {
if x < 0 {
return falseFilter
}
if len(ignores) < 0 {
return filterFunc(func(runes []rune) bool {
return x < len(runes) && runes[x] != r && containsRune(runes, r)
})
}
return filterFunc(func(runes []rune) bool {
if x >= len(runes) || runes[x] == r {
return false
}
rr := make([]rune, len(runes))
copy(rr, runes)
for _, n := range ignores {
rr[n] = 0
}
return containsRune(rr, r)
})
}
func ApplyFilter(f Filter, src []string) []string {
dst := make([]string, 0, len(src) / 2)
for _, s := range src {
if f.Filter(s) {
dst = append(dst, s)
}
}
return dst
}