Coding Challenge – Advent of Code

Although my true passion will always be fishing, I’ve started bow hunting this year. It isn’t quite as fun (although maybe it’ll get better once I actually see some deer), but it’s still a nice way to get out and enjoy nature. Especially during this time of year when it’s too cold to launch a boat but not yet cold enough to ice fish, it’s great to have another way to get out of the house. I’ve found that muskie fishing is probably closer to deer hunting than it is to other types of fishing; you are waiting all day for just the chance to see a muskie/deer, but when you do it’s very exciting. It takes a great deal of though and preparation to be successful.

I was recently shown a set of challenges called Advent of Code. Its a fun way to practice coding and keep your skills fresh. I mainly code in SQL, but used to do quite a bit in Access and Excel VBA, and a small amount in C# and Powershell. I’d like to use this Advent of Code challenge to brush up on my skills and hopefully learn a bit more about Powershell, which can be a big asset in administering SQL Server databases.
The first challenge is to interpret a set of instructions where the parentheses symbols are used to tell Santa to go up or down in a house; right paranthesis “(” tells Santa to go up a floor, left “)” tells him to go down a level. In the two part question, we need to figure out first what floor he ends up at and second to figure out which step first takes him below level 0, into the basement. I solved this one by pasting the instructions, which was 7000 characters long, into a cell in Excel and then running some VBA code to get the answers. I got both parts correct on my first try. Here’s the code I used:

Sub Solver()

    Dim i As Integer
    Dim flr As Integer
    
    For i = 1 To Len(Me.Cells(1, 1).Value)
        If Mid(Me.Cells(1, 1).Value, i, 1) = "(" Then
            flr = flr + 1
        Else
            flr = flr - 1
        End If
    Next i
    
    Debug.Print flr

End Sub
Sub Solver2()

    Dim i As Integer
    Dim flr As Integer
    
    For i = 1 To Len(Me.Cells(1, 1).Value)
        If Mid(Me.Cells(1, 1).Value, i, 1) = "(" Then
            flr = flr + 1
        Else
            flr = flr - 1
        End If
        If flr = -1 Then
            Debug.Print i
            Exit Sub
        End If
    Next i
    

End Sub

If you’re looking for a fun challenge or a way to grow your coding skills, definitely check out the Advent of Code.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s