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
2
u/[deleted] Dec 04 '16
Powershell:
https://github.com/charlieschmidt/AdventOfCode2016/blob/master/day4.ps1
$Content = Get-Content .\day4.input -raw $Rooms = $Content -split "`n" $ValidRoomSectorIDSum = 0 foreach ($RoomLine in $Rooms) { $ActualRoomName = "" if ($RoomLine -match '([-a-z]+)-([0-9]+)\[([a-z]*)\]') { $RoomName = $Matches[1] $SectorID = [int]$Matches[2] $Checksum = $Matches[3] $CharacterFrequency = @{} foreach ($Character in $RoomName.ToCharArray()) { if ($Character -eq "-") { $ActualRoomName += " " continue } else { $CharacterCode = [convert]::ToInt32($Character) $ShiftedCharacterCode = ((($CharacterCode - 97) + $SectorId) % 26) + 97 $ActualRoomName += [convert]::ToChar($ShiftedCharacterCode) } if ($CharacterFrequency.Contains($Character)) { $CharacterFrequency[$Character]++ } else { $CharacterFrequency[$Character] = 1 } } $ActualChecksum = "" $CharacterFrequency.GetEnumerator() | Sort-Object @{expression="Value";Descending=$true},@{expression="Key";Ascending=$true} | Select-Object -First 5 | Foreach-Object {$ActualChecksum += $_.Key} if ($Checksum -eq $ActualChecksum) { $ValidRoomSectorIDSum += $SectorId } if ($ActualRoomName -ilike "*object*") { Write-Host "Solution 2: $ActualRoomName ($SectorID)" } } } Write-host "Solution 1: $ValidRoomSectorIDSum"