-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11_part2.go
73 lines (63 loc) · 1.02 KB
/
day11_part2.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
package day11_part2
import (
"fmt"
"strings"
)
func getAnswer(input string) string {
password := input
i := 0
n := 0
for {
password = incrementPassword(password)
ok := isPasswordValid(password)
if ok {
n++
fmt.Printf("Output: %d = %q\n", i, password)
if n == 2 { // comment this out for part 1
return password
}
}
i++
}
return ""
}
func incrementPassword(password string) string {
runes := []rune(password)
i := len(runes) - 1
for {
if runes[i] == 'z' {
runes[i] = 'a'
i--
continue
}
runes[i]++
break
}
return string(runes)
}
func isPasswordValid(password string) bool {
ok := false
for i := 0; i < len(password)-2; i++ {
if password[i] == password[i+1]-1 && password[i+1] == password[i+2]-1 {
ok = true
break
}
}
if !ok {
return false
}
if strings.ContainsAny(password, "iol") {
return false
}
pairs := 0
for i := 0; i < len(password)-1; i++ {
if password[i] == password[i+1] {
pairs++
i += 1
}
}
if pairs < 2 {
return false
}
return true
}