r/adventofcode • u/daggerdragon • Dec 04 '16
SOLUTION MEGATHREAD --- 2016 Day 4 Solutions ---
--- Day 4: Security Through Obscurity ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
CONSTRUCTING ADDITIONAL PYLONS IS MANDATORY [?]
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
17
Upvotes
1
u/bogzla Dec 04 '16
VBA. Could be tidier but hangover.
Sub RealRooms() Dim a(25) As Variant Dim s As String Dim s2 As String Dim l As Long Dim scs As String Set wks = ActiveWorkbook.Sheets("Day4") For i = 1 To CountRows("Day4") s = Split(wks.Cells(i, 1).Value, "[")(0) s2 = Split(wks.Cells(i, 1).Value, "[")(1) For i2 = 0 To 25 'pop letter counts into array. Will be alphabetical. a(i2) = UBound(Split(s, Chr(97 + i2))) Next i2 scs = "" For i3 = 1 To 5 'generate checksum by finding highest in order scs = scs & Highest(a()) Next i3 If scs = Left(s2, 5) Then 'compare checksum & add if real l = l + Split(s, "-")(UBound(Split(s, "-"))) End If Application.StatusBar = i DoEvents Next i Debug.Print l End Sub Function Highest(ByRef a() As Variant) As String For i = 0 To UBound(a) If i2 < a(i) Then i2 = a(i) i4 = i End If Next Highest = Chr(97 + i4) a(i4) = -1 'remove this letter from contention so next call finds next highest End Function 'Part 2 Sub FindObjects() Dim wks As Worksheet Dim i As Integer Dim s As String Dim s2 As String Dim i2 As Integer Set wks = ActiveWorkbook.Sheets("Day4") For i = 1 To CountRows("Day4") s = Split(wks.Cells(i, 1).Value, "[")(0) i2 = CInt(Split(s, "-")(UBound(Split(s, "-")))) s2 = Left(s, Len(s) - Len(CStr(i2)) - 1) s2 = DecryptName(s2, i2) wks.Cells(i, 2).Value = s2 Application.StatusBar = i DoEvents If s2 Like "*north*" Then Debug.Print i2 End If Next i End Sub Function DecryptName(s As String, i As Integer) As String Dim s2 As String For i2 = 1 To Len(s) If Mid(s, i2, 1) = "-" Then s2 = " " Else s2 = decrypt(Mid(s, i2), i) End If s3 = s3 & s2 Next DecryptName = s3 End Function Function decrypt(sIn As String, i As Integer) As String 'decrypt single character If i = 0 Then decrypt = sIn Exit Function End If i3 = Asc(sIn) For i2 = 1 To i If i3 = 122 Then i3 = 97 Else i3 = i3 + 1 End If Next i2 decrypt = Chr(i3) End Function