Wednesday, December 26, 2018

Advent of Code 2015 - Day 11: Corporate Policy

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 to xyz. They cannot skip letters; abd doesn't count.
  • Passwords may not contain the letters i, o, or l, 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, or zz.
For example:
  • hijklmmn meets the first requirement (because it contains the straight hij) but fails the second requirement requirement (because it contains i and l).
  • abbceffg meets the third requirement (because it repeats bb and ff) but fails the first requirement.
  • abbcegjk fails the third requirement, because it only has one double letter (bb).
  • The next password after abcdefgh is abcdffaa.
  • The next password after ghijklmn is ghjaabcc, because you eventually skip all the passwords that start with ghi..., since i is not allowed.
Given Santa's current password (your puzzle input), what should his next password be?

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

Part 2

Description

Santa's password expired again. What's the next one?

Solution

For the part 2, I just need to modify the input in the code with output of the part 1.

Share:

0 komentar:


Recent Posts

Categories

Blog Archive