Dag: 1
Språk: #powershell
Tyvärr har jag inte så bra med tid men ska försöka hinna med några dagar i alla fall. Körde för något år sedan med #PowerShell men då gjorde jag stora funktioner. Denna gången vill jag slimma ned radantalet så mycket som möjligt.
## Part 1
$Input_D1 = Get-Content -Path .\Input.txt
$increase = 0; $decrease = 0; $same = 0
for ($i = 0-1; $i -lt $Input_D1.Count; $i++) {
switch( $Input_D1[$i] ){
{ $Input_D1[$i] -gt $Input_D1[$i-1] } { $increase++; Write-Verbose "$($Input_D1[$i]) < $($Input_D1[$i-1])" }
{ $Input_D1[$i] -lt $Input_D1[$i-1] } { $decrease++; Write-Verbose "$($Input_D1[$i]) > $($Input_D1[$i-1])" }
default { $same++; Write-Verbose "$($Input_D1[$i]) = $($Input_D1[$i-1])" }
}
}
return $increase
## Part 2
$increase3 = 0; $decrease3 = 0; $same3 = 0
for ($i3 = 0; $i3 -lt $Input_D1.Count; $i3++) {
if(-not [string]::IsNullOrEmpty($Input_D1[$i3+3])){
$curr3 = $null; $curr3 = ($(@($Input_D1[$i3,($i3+1),($i3+2)])) | Measure-Object -Sum).Sum
$next3 = $null; $next3 = ($(@($Input_D1[($i3+1),($i3+2),($i3+3)])) | Measure-Object -Sum).Sum
switch( $curr3 ){
{ $curr3 -lt $next3 } { $increase3++; Write-Verbose "$curr3 < $next3" }
{ $curr3 -gt $next3 } { $decrease3++; Write-Verbose "$curr3 > $next3" }
default { $same3++; Write-Verbose "$curr3 = $next3" }
}
}
}
return $increase3
Dag: 2
Språk: #powershell
## Part 1
try{
$Input_D2 = Get-Content -Path .\Input.txt
$forward = 0; $depth = 0
foreach($item in $Input_D2) {
switch( ($item -split ' ')[0] ){
'forward' { $forward = $forward + [int]$(($item -split ' ')[1]) }
'up' { $depth = $depth - [int]$(($item -split ' ')[1]) }
'down' { $depth = $depth + [int]$(($item -split ' ')[1]) }
default { Write-Host "Unable to match direction!!" -ForegroundColor Red; throw "Check code!"}
}
}
return $forward*$depth
## Part 2
$forward_2 = 0; $depth_2 = 0; $aim_2 = 0
foreach($item_2 in $Input_D2) {
$p1 = ($item_2 -split ' ')[0]
$p2 = ($item_2 -split ' ')[1]
switch( $p1 ){
'forward' { $forward_2 += [int]$p2
$depth_2 += [int]$p2*$aim_2
}
'up' { $aim_2 -= [int]$p2 }
'down' { $aim_2 += [int]$p2 }
default { Write-Host "Unable to match direction!!" -ForegroundColor Red; throw "Check code!"}
}
}
return $forward_2*$depth_2
} catch {
Write-Host -ForegroundColor Red "Check code!"
}