-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvent11.go
65 lines (57 loc) · 1.23 KB
/
advent11.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
package main
import "fmt"
import "strings"
func incrementPassword(s string) string {
r := []rune(s)
for i := len(r) - 1; i >= 0; i-- {
r[i]++
if r[i] <= 'z' {
break
}
// roll over
r[i] = 'a'
}
return string(r)
}
func hasRunOfThree(pw string) bool {
// Check for runs of three
for i := 0; i < len(pw)-2; i++ {
if pw[i+1] == pw[i]+1 && pw[i+2] == pw[i]+2 {
return true
}
}
return false
}
func hasTwoDoubles(pw string) bool {
var dblCount int
for i := 0; i < len(pw)-1; i++ {
if pw[i] == pw[i+1] {
i++
dblCount++
if dblCount >= 2 {
return true
}
}
}
return false
}
func hasIllegalLetter(pw string) bool {
return strings.ContainsAny(pw, "iol")
}
func isLegalPassword(pw string) bool {
return hasRunOfThree(pw) && hasTwoDoubles(pw) && !hasIllegalLetter(pw)
}
func nextLegalPassword(pw string) string {
next := incrementPassword(pw)
for !isLegalPassword(next) {
next = incrementPassword(next)
}
return next
}
func main() {
password := "vzbxkghb"
next_pw := nextLegalPassword(password)
next_next_pw := nextLegalPassword(next_pw)
fmt.Println(next_pw)
fmt.Println(next_next_pw)
}