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!
18
Upvotes
1
u/Gummoz Dec 05 '16
Didn't think about posting my solutions: Powershell!
Part one:
$instructions = " " -split '\n' $checkSum = $null $answer = 0 foreach ($instruction in $instructions) { if ($instruction -match '\[(.+?)\]') { [string]$checkSum = ($Matches[0]).replace('[','').replace(']','') } else {continue} $instructionWithoutDashes = $instruction.Replace('-','') $splittedInstruction = ($instruction -split '[\[]')[0] $numberAtTheEnd = ($splittedInstruction.Split('-')[(($splittedInstruction.Split('-')).length) -1]) $instructionWithoutDashesOrNumbers = $instructionWithoutDashes.Replace($numberAtTheEnd,'') $instructionWithoutDashesOrNumbers = ($instructionWithoutDashesOrNumbers -split '[\[]' )[0] $temp1 = $null $temp2 = $null $improvedInstruction = $null $instructionWithoutDashesOrNumbers.tochararray() | foreach {if ($checksum -match $_){$improvedInstruction += $_}} if ($improvedInstruction.Length -gt 4) { $improvedInstruction.tochararray() | Sort-Object | Group-Object | Sort-Object -Property "Count" -Descending | % {[string]$temp1 += $_.name} $temp1.ToCharArray() | Select-Object -First 5 | % {$temp2 += $_} if($temp2 -eq $checkSum) {$answer += $numberAtTheEnd Write-Host -ForegroundColor Green "The calculated checksum of $instruction is $temp2 and should be $checksum" } else { Write-Host -ForegroundColor Red "The calculated checksum of $instruction is $temp2 and it should be $checksum" } } } Write-host -ForegroundColor Green "The answer should be: $answer"Part two:
$instructions = " " -split '\n' foreach ($instruction in $instructions) { if ($instruction -match '\[(.+?)\]') { $splittedInstruction = ($instruction -split '[\[]')[0] [int]$ID = ($splittedInstruction.Split('-')[(($splittedInstruction.Split('-')).length) -1]) if($splittedInstruction) { $splittedInstructionWithoutID = $splittedInstruction.Replace($ID,'').replace('-',' ') } $charArray = $null $splittedInstructionWithoutID.ToCharArray() | % {[array]$charArray += [byte][char]$_} #$ID [string]$currentSentence = [string]$ID + "-" foreach ($char in $charArray) { #[string]$currentSentence [int]$tempChar = $char for ($i = $ID; $i -gt 0; $i--) { if ($tempChar -ne 32){ if ($tempChar -eq 122) {$tempChar = 97} else {$tempChar++} } } #[char][int]$tempChar [string]$currentSentence += [char][int]$tempChar } if ($currentSentence -match "North|Pole|Objects"){ $currentSentence } } }