LabtechConsulting
12/2/2016 - 6:44 AM

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

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

$List = (Invoke-WebRequest 'https://gist.githubusercontent.com/LabtechConsulting/8c7feb79dc94f656a43a725d7ec5f940/raw/7629014a459b9de01bd90ed2c27338e723144faa/new_gist_file_0').Content
$List = $List -split '[\r\n]' | where {$_}

function Bathroom-Code{
    param(
        [parameter(ValueFromPipeline=$True)]
        $Instruction        
    )
    begin{
        $x = -2
        $y = 0
        $Code = @()
    }
    process{
        $Instruction = $Instruction.ToCharArray() | where {$_ -match '\w'}
        foreach($Dir in $Instruction){
            if($x -in (2,-2) -or $y -in (2,-2)){
                if($x -eq 0 -and $y -eq 2 -and $Dir -eq 'd'){$y = 1}
                elseif($x -eq 0 -and $y -eq -2 -and $Dir -eq 'u'){$y = -1}
                elseif($x -eq 2 -and $y -eq 0 -and $Dir -eq 'l'){$x = 1}
                elseif($x -eq -2 -and $y -eq 0 -and $Dir -eq 'r'){$x = -1}
            }
            else{
                switch($Dir) {
                    'r'{ $x += 1}
                    'l'{ $x -= 1}
                    'u'{ $y += 1}
                    'd'{ $y -= 1}
                }
                if($x -gt 1 -and $y -ne 0){$x=1}
                if($x -gt 2){$x=2}
                if($x -lt -1 -and $y -ne 0){$x=-1}
                if($x -lt -2){$x=-2}
                if($y -gt 1 -and $x -ne 0){$y=1}
                if($y -gt 2){$y=2}
                if($y -lt -1 -and $x -ne 0){$y=-1}
                if($y -lt -2){$y=-2}
            }
            switch("$x$y"){
                '02'{ $Key = 1}
                '-11'{ $Key = 2}
                '01'{ $Key = 3}
                '11'{ $Key = 4}
                '-20'{ $Key = 5}
                '-10'{ $Key = 6}
                '00'{ $Key = 7}
                '10'{ $Key = 8}
                '20'{ $Key = 9}
                '-1-1'{ $Key = 'A'}
                '0-1'{ $Key = 'B'}
                '1-1'{ $Key = 'C'}
                '0-2'{ $Key = 'D'}
            }
        }
        $Code += $Key
    } 
    end{$Code -join ''}    
}
$list | Bathroom-Code