Advent of Code 2016 Day 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 = 0
$y = 0
$Code = @()
}
process{
$Instruction = $Instruction.ToCharArray() | where {$_ -match '\w'}
foreach($Dir in $Instruction){
switch($Dir) {
'r'{ $x += 1}
'l'{ $x -= 1}
'u'{ $y += 1}
'd'{ $y -= 1}
}
if($x -gt 1){$x=1}
if($x -lt -1){$x=-1}
if($y -gt 1){$y=1}
if($y -lt -1){$y=-1}
switch("$x$y"){
'-11'{ $Key = 1}
'01'{ $Key = 2}
'11'{ $Key = 3}
'-10'{ $Key = 4}
'00'{ $Key = 5}
'10'{ $Key = 6}
'-1-1'{ $Key = 7}
'0-1'{ $Key = 8}
'1-1'{ $Key = 9}
}
}
$Code += $Key
}
end{$Code -join ''}
}
$list | Bathroom-Code