LabtechConsulting
12/1/2016 - 7:01 AM

Advent of Code 2016 Day 1 Part 1 http://adventofcode.com/2016/day/1

Advent of Code 2016 Day 1 Part 1 http://adventofcode.com/2016/day/1

$List = (Invoke-WebRequest 'https://gist.githubusercontent.com/LabtechConsulting/10dcec9713a56830b178d4bfec5a5b75/raw/bd57f1335350a6f76e3292a4a124a338e04ee529/new_gist_file_0').Content
$List = ($List -split ',').trim()

function Find-HQ{
    param(
        [parameter(ValueFromPipeline=$True)]
        $Instruction,
        $Dir

    )
    begin{
        $x = 0
        $y = 0
        switch($Dir) {
            'n'{ $Dir = 0}
            'e'{ $Dir = 1}
            's'{ $Dir = 2}
            'w'{ $Dir = 3}
        }
    }
    process{
        $Steps = [convert]::ToInt32($Instruction.trim('LR'), 10)
        $Direction = $Instruction.replace($Steps,'')
        if($Direction -eq 'r'){$Dir ++}
        else{$Dir --}
        if($Dir -gt 3){$Dir = 0}
        if($Dir -lt 0){$Dir = 3}
        switch($Dir) {
            1{ $x += $Steps}
            3{ $x -= $Steps}
            0{ $y += $Steps}
            2{ $y -= $Steps}
        }
    } 
    end{
        Write-Output "HQ at x:$x y:$y"
        if($x -lt 0){$x *= -1}
        if($y -lt 0){$y *= -1}
        $Total = $x + $y
        Write-Output "Bunny is $Total blocks away"
    }    
}

$list | Find-HQ -Dir n