Part 1
Description
Santa's previous password expired, and he needs help choosing a new one.To help him remember his new password after the old one expires, Santa has devised a method of coming up with a password based on the previous one. Corporate policy dictates that passwords must be exactly eight lowercase letters (for security reasons), so he finds his new password by incrementing his old password string repeatedly until it is valid.
Incrementing is just like counting with numbers:
xx
, xy
, xz
, ya
, yb
, and so on. Increase the rightmost letter one step; if it was z
, it wraps around to a
, and repeat with the next letter to the left until one doesn't wrap around.Unfortunately for Santa, a new Security-Elf recently started, and he has imposed some additional password requirements:
- Passwords must include one increasing straight of at least three letters, like
abc
,bcd
,cde
, and so on, up toxyz
. They cannot skip letters;abd
doesn't count. - Passwords may not contain the letters
i
,o
, orl
, as these letters can be mistaken for other characters and are therefore confusing. - Passwords must contain at least two different, non-overlapping pairs of letters, like
aa
,bb
, orzz
.
hijklmmn
meets the first requirement (because it contains the straighthij
) but fails the second requirement requirement (because it containsi
andl
).abbceffg
meets the third requirement (because it repeatsbb
andff
) but fails the first requirement.abbcegjk
fails the third requirement, because it only has one double letter (bb
).- The next password after
abcdefgh
isabcdffaa
. - The next password after
ghijklmn
isghjaabcc
, because you eventually skip all the passwords that start withghi...
, sincei
is not allowed.
Input
Solution
It might be possible to solve this problem without using VBA. But, I guess it will require a lot of space. So, I decided to use VBA. My code was not that neat. It was too wordy. So, any suggestion to make it more concise and efficient is much more appreciated. Here is my code, with two sub function:Sub CorporatePolicy() Dim input11 As String Dim p1, p2, p3, p4, p5, p6, p7, p8 As String Dim amazon(1 To 26) As Variant Dim i, j, k, l, m, n, o, p As Integer input11 = "hxbxwxba" For i = (Asc(Mid(input11, 1, 1)) - 96) To 26 p1 = Mid(input11, 1, 1) For j = (Asc(Mid(input11, 2, 1)) - 96) To 26 p2 = Mid(input11, 2, 1) For k = (Asc(Mid(input11, 3, 1)) - 96) To 26 p3 = Mid(input11, 3, 1) For l = (Asc(Mid(input11, 4, 1)) - 96) To 26 p4 = Mid(input11, 4, 1) For m = (Asc(Mid(input11, 5, 1)) - 96) To 26 p5 = Mid(input11, 5, 1) For n = (Asc(Mid(input11, 6, 1)) - 96) To 26 p6 = Mid(input11, 6, 1) For o = (Asc(Mid(input11, 7, 1)) - 96) To 26 p7 = Mid(input11, 7, 1) For p = (Asc(Mid(input11, 8, 1)) - 96) To 26 p8 = nextchar(Mid(input11, 8, 1)) input11 = Left(input11, 7) & p8 If isvalid(p1, p2, p3, p4, p5, p6, p7, p8, input11) Then Debug.Print input11 Exit Sub End If Next p p7 = nextchar(p7) input11 = Left(input11, 6) & p7 & p8 If isvalid(p1, p2, p3, p4, p5, p6, p7, p8, input11) Then Debug.Print input11 Exit Sub End If Next o p6 = nextchar(p6) input11 = Left(input11, 5) & p6 & p7 & p8 If isvalid(p1, p2, p3, p4, p5, p6, p7, p8, input11) Then Debug.Print input11 Exit Sub End If Next n p5 = nextchar(p5) input11 = Left(input11, 4) & p5 & p6 & p7 & p8 If isvalid(p1, p2, p3, p4, p5, p6, p7, p8, input11) Then Debug.Print input11 Exit Sub End If Next m p4 = nextchar(p4) input11 = Left(input11, 3) & p4 & p5 & p6 & p7 & p8 If isvalid(p1, p2, p3, p4, p5, p6, p7, p8, input11) Then Debug.Print input11 Exit Sub End If Next l p3 = nextchar(p3) input11 = Left(input11, 2) & p3 & p4 & p5 & p6 & p7 & p8 If isvalid(p1, p2, p3, p4, p5, p6, p7, p8, input11) Then Debug.Print input11 Exit Sub End If Next k p2 = nextchar(p2) input11 = Left(input11, 1) & p2 & p3 & p4 & p5 & p6 & p7 & p8 If isvalid(p1, p2, p3, p4, p5, p6, p7, p8, input11) Then Debug.Print input11 Exit Sub End If Next j p1 = nextchar(p1) input11 = p1 & p2 & p3 & p4 & p5 & p6 & p7 & p8 If isvalid(p1, p2, p3, p4, p5, p6, p7, p8, input11) Then Debug.Print input11 Exit Sub End If Next i End Sub
Function nextchar(ini) As String If ini = "z" Then nextchar = "a" Else nextchar = Chr(1 + Asc(ini)) End If End Function
Function isvalid(ByVal p1, p2, p3, p4, p5, p6, p7, p8 As String, pass As String) As Boolean If ((Asc(p1) + 1 = Asc(p2) And Asc(p2) + 1 = Asc(p3)) Or (Asc(p2) + 1 = Asc(p3) And Asc(p3) + 1 = Asc(p4)) Or _ (Asc(p3) + 1 = Asc(p4) And Asc(p4) + 1 = Asc(p5)) Or (Asc(p4) + 1 = Asc(p5) And Asc(p5) + 1 = Asc(p6)) Or _ (Asc(p5) + 1 = Asc(p6) And Asc(p6) + 1 = Asc(p7)) Or (Asc(p6) + 1 = Asc(p7) And Asc(p7) + 1 = Asc(p8))) And _ (Len(Replace(Replace(Replace(pass, "i", ""), "o", ""), "l", "")) = 8) And _ ((p1 = p2 And (p3 = p4 Or p4 = p5 Or p5 = p6 Or p6 = p7 Or p7 = p8)) Or (p2 = p3 And (p4 = p5 Or p5 = p6 Or p6 = p7 Or p7 = p8)) Or _ (p3 = p4 And (p5 = p6 Or p6 = p7 Or p7 = p8)) Or (p4 = p5 And (p6 = p7 Or p7 = p8)) Or (p5 = p6 And p7 = p8)) _ Then isvalid = True End If End Function
0 komentar:
Post a Comment