-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
104 lines (84 loc) · 1.89 KB
/
main.go
File metadata and controls
104 lines (84 loc) · 1.89 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
package main
import (
"github.com/danvolchek/AdventOfCode/lib"
)
func hasIncreasing(password []byte) bool {
for i := 0; i < len(password)-2; i++ {
if password[i+1]-password[i] == 1 && password[i+2]-password[i+1] == 1 {
return true
}
}
return false
}
func noConfusing(password []byte) bool {
for _, char := range password {
switch char {
case 'i', 'o', 'l':
return false
}
}
return true
}
func twoPairs(password []byte, hasFirst bool) bool {
for i := 0; i < len(password)-1; i++ {
if password[i] == password[i+1] {
if hasFirst {
return true
}
if twoPairs(password[i+2:], true) {
return true
}
}
}
return false
}
func valid(password []byte) bool {
return noConfusing(password) && hasIncreasing(password) && twoPairs(password, false)
}
func increment(password []byte) []byte {
for index := len(password) - 1; index > -1; index-- {
switch password[index] {
case 'z':
password[index] = 'a'
default:
password[index] += 1
}
if password[index] != 'a' {
break
}
}
// note: just for the step solver; improve this?
return password
}
func solve(password []byte) string {
increment(password)
for !valid(password) {
increment(password)
}
return string(password)
}
func main() {
incrementTest := lib.Solver[[]byte, string]{
ParseF: lib.ParseBytes,
SolveF: lib.ToString(increment),
}
incrementTest.Expect("a", "b")
incrementTest.Expect("xx", "xy")
incrementTest.Expect("xy", "xz")
incrementTest.Expect("xz", "ya")
incrementTest.Expect("ya", "yb")
validTest := lib.Solver[[]byte, bool]{
ParseF: lib.ParseBytes,
SolveF: valid,
}
validTest.Expect("hijklmmn", false)
validTest.Expect("abbceffg", false)
validTest.Expect("abbcegjk", false)
solver := lib.Solver[[]byte, string]{
ParseF: lib.ParseBytes,
SolveF: solve,
}
solver.Expect("abcdefgh", "abcdffaa")
solver.Expect("ghijklmn", "ghjaabcc")
solver.Verify("cqjxxyzz")
}