diff --git a/Advent Of Code Day 12-part2.ps1 b/Advent Of Code Day 12-part2.ps1 new file mode 100644 index 0000000..be1f76e --- /dev/null +++ b/Advent Of Code Day 12-part2.ps1 @@ -0,0 +1,105 @@ +$cookievalue = get-content .\Cookie.txt +$cookie = New-Object system.net.cookie +$cookie.name="session" +$cookie.domain = "adventofcode.com" +$cookie.Value=$cookievalue +$websession = new-object Microsoft.PowerShell.Commands.WebRequestSession +$websession.cookies.add($cookie) +$inputdata = (Invoke-RestMethod -Uri https://adventofcode.com/2020/day/12/input -Method GET -WebSession $websession) -split "\r?\n" | where-object {$_ -notlike ''} +set-content -Path .\aocd12input.txt -Value $inputdata + +$position = @{ + XAxis=0; + YAxis=0; + Facing=@{ + 0="E"; + 1="N"; + 2="W"; + 3="S"; + }; + FTurn=0; +} +$outerbounds = @{ + MaxXAxis=0; + MinXAxis=0; + MaxYAxis=0; + MinYAxis=0; +} +function Update-Facing (){ + param( + $numberOfTurns + ) + #write-host $numberOfTurns + if($numberOfTurns -lt 0){ + $tempnum = 4 + $numberOfTurns + $numberofturns = [math]::abs($tempnum) + } + $currentfacing = $position.fturn + $position.fturn = ($currentfacing + $numberOfTurns) % 4 +} +function Update-MaxDistance(){ + #$position + if($position.XAxis -gt $outerbounds.MaxXAxis){ + $outerbounds.MaxXAxis=$position.XAxis + } + if($position.YAxis -gt $outerbounds.MaxYAxis){ + $outerbounds.MaxYAxis=$position.YAxis + } + if($position.XAxis -lt $outerbounds.MinXAxis){ + $outerbounds.MinXAxis=$position.XAxis + } + if($position.YAxis -lt $outerbounds.MinYAxis){ + $outerbounds.MinYAxis=$position.YAxis + } +} +function Move-Direction(){ + param( + $axis, + $distance + ) + if($axis -eq "XAxis"){ + $position.XAxis += $distance + } + if($axis -eq "YAxis"){ + $position.YAxis += $distance + } +} +foreach($line in $inputdata){ + $direction = $line.ToCharArray()[0] + [int]$distance = $line.ToCharArray()[1..$line.Length] -join "" + switch ($direction){ + "N"{ + Move-Direction -axis "YAxis" -distance $distance + }; + "S"{ + + Move-Direction -axis "YAxis" -distance (-$distance) + + }; + "E"{ + Move-Direction -axis "XAxis" -distance $distance + }; + "W"{ + Move-Direction -axis "XAxis" -distance (-$distance) + }; + "L"{ + update-facing -numberOfTurns ($distance/90) + }; + "R"{ + update-facing -numberOfTurns (-$distance/90) + }; + "F"{ + if($position.facing[$position.fturn] -eq "N"){ + Move-Direction -axis "YAxis" -distance $distance + } elseif($position.facing[$position.fturn] -eq "S"){ + Move-Direction -axis "YAxis" -distance (-$distance) + } elseif($position.facing[$position.fturn] -eq "E"){ + Move-Direction -axis "XAxis" -distance $distance + } elseif($position.facing[$position.fturn] -eq "W"){ + Move-Direction -axis "XAxis" -distance (-$distance) + } + }; + } + Update-MaxDistance +} +[math]::abs($position.XAxis) + [MATH]::abs($position.YAxis) \ No newline at end of file diff --git a/Advent Of Code Day 9-Combined.ps1 b/Advent Of Code Day 9-Combined.ps1 index dfe9af9..1586c1d 100644 --- a/Advent Of Code Day 9-Combined.ps1 +++ b/Advent Of Code Day 9-Combined.ps1 @@ -1,3 +1,4 @@ +get-date $inputdata=import-csv -header "Number" .\aocd9input.txt $inputdata | %{$_.Number = [int64]$_.Number} $buffer=25 @@ -65,4 +66,5 @@ foreach($num in ($howmanynums .. $inputdata.count)){ } #write-host "$mynums : $mysum" } -} \ No newline at end of file +} +get-date \ No newline at end of file diff --git a/Advent Of Code Day 9-part1.ps1 b/Advent Of Code Day 9-part1.ps1 index 208ac86..e85ff94 100644 --- a/Advent Of Code Day 9-part1.ps1 +++ b/Advent Of Code Day 9-part1.ps1 @@ -34,4 +34,5 @@ for($i=25; $i -lt $inputdata.count; $i++){ } $null,$mynumbers=$mynumbers $mynumbers += $newnum -} \ No newline at end of file +} +get-date \ No newline at end of file