Advent of Code 2016 Day 1 Part 2 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()
$hash = @{}
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}
}
$Found = 0
$hash[[tuple]::Create($x,$y)] = 1
}
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}
for ($i=1; $i -le $Steps -and $Found -ne 1; $i++){
switch($Dir) {
1{ $x += 1}
3{ $x -= 1}
0{ $y += 1}
2{ $y -= 1}
}
$hash[[tuple]::Create($x,$y)] += 1
if($hash.ContainsValue(2)){
$Found = 1
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"
}
}
if($Found -eq 1){continue}
}
end{}
}
$list | Find-HQ -Dir n