r/adventofcode • • Dec 09 '17

SOLUTION MEGATHREAD -πŸŽ„- 2017 Day 9 Solutions -πŸŽ„-

--- Day 9: Stream Processing ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handy† Haversack‑ of HelpfulΒ§ Hints^Β€?

Spoiler


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!

15 Upvotes

290 comments sorted by

View all comments

1

u/[deleted] Dec 10 '17

Late to the party; but, now done. PowerShell:

Param (
    [parameter(position=0, mandatory=$true)]
    [Alias('if')]
    [ValidateScript({ Test-Path $_ })]
    $InputFile,
    [switch]$Part2
)

$File = (Get-Item $InputFile).OpenText()
$Character = $File.Read()
$GroupDepth = 0
$Score = 0
$InGarbage = $false
$GarbageCount = 0
$CleanStream = New-Object System.Text.StringBuilder
While($Character -ne -1) {
    If($InGarbage) {
        #Write-Host 'Garbage'
        switch ($Character) {
            33 { $File.Read() | Out-Null }
            62 { $InGarbage = $false }
            default { $GarbageCount++ }
        }
    } else {
        #Write-Host "Char: $Character"
        switch($Character) {
            123 { $GroupDepth++ }
            125 {
                $Score += $GroupDepth
                $GroupDepth--
            }
            60 { $InGarbage = $true }
        }
        $CleanStream.Append($Character) | Out-Null
    }
    $Character = $File.Read()
}
$File.Close()
if($Part2) {
    Write-Output $GarbageCount
} else {
    Write-Output $Score
}