mirror of
https://github.com/paradizelost/AdventOfCode2020.git
synced 2024-11-25 02:44:45 -06:00
catchup
This commit is contained in:
parent
a7b4ed6dbd
commit
2bd31cb252
105
Advent Of Code Day 12-part2.ps1
Normal file
105
Advent Of Code Day 12-part2.ps1
Normal file
@ -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)
|
@ -1,3 +1,4 @@
|
||||
get-date
|
||||
$inputdata=import-csv -header "Number" .\aocd9input.txt
|
||||
$inputdata | %{$_.Number = [int64]$_.Number}
|
||||
$buffer=25
|
||||
@ -66,3 +67,4 @@ foreach($num in ($howmanynums .. $inputdata.count)){
|
||||
#write-host "$mynums : $mysum"
|
||||
}
|
||||
}
|
||||
get-date
|
@ -35,3 +35,4 @@ for($i=25; $i -lt $inputdata.count; $i++){
|
||||
$null,$mynumbers=$mynumbers
|
||||
$mynumbers += $newnum
|
||||
}
|
||||
get-date
|
Loading…
Reference in New Issue
Block a user