-
Notifications
You must be signed in to change notification settings - Fork 1
/
11.go
62 lines (56 loc) · 1.48 KB
/
11.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
package main
import (
"fmt"
"strings"
)
func increment(text string) string {
if len(text) == 0 {
return "a"
}
lastIndex := len(text) - 1
if text[lastIndex] < byte('z') {
return text[:lastIndex] + string(text[lastIndex]+1)
}
return increment(text[:lastIndex]) + "a"
}
func validate(text string) bool {
// Passwords may not contain the letters i, o, or l, as these letters can be mistaken for other characters and are therefore confusing.
if strings.ContainsAny(text, "iol") {
return false
}
// Passwords must contain at least two different, non-overlapping pairs of letters, like aa, bb, or zz.
doubles := ""
for i := 0; i < len(text)-1; i++ {
if text[i] == text[i+1] {
if !strings.Contains(doubles, string(text[i])) {
doubles += string(text[i])
// skip 1 to avoid 'aaa' being counted twice
i++
}
}
}
if len(doubles) < 2 {
return false
}
// Passwords must include one increasing straight of at least three letters, like abc, bcd, cde, and so on, up to xyz. They cannot skip letters; abd doesn't count.
for i := 0; i < len(text)-3; i++ {
if text[i]+1 == text[i+1] && text[i]+2 == text[i+2] {
return true
}
}
return false
}
func nextPassword(current string) string {
for {
current = increment(current)
if validate(current) {
return current
}
}
}
func main() {
password := "vzbxkghb"
newPass := nextPassword(password)
fmt.Println(newPass)
fmt.Println(nextPassword(newPass))
}