Last active
December 23, 2020 16:28
-
-
Save ArtisanByteCrafter/ab6ddc33924060dd2eee277977717867 to your computer and use it in GitHub Desktop.
Advent Of Code Day 5 2020
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------- | |
# PART 1 | |
#------- | |
# input file | |
$plane=Get-Content "/Users/{username}/Downloads/AdventOfCode_Day5.txt" | |
Function Get-Seat { | |
[cmdletbinding()] | |
param( | |
[Parameter(Mandatory)] | |
[string] $Seat | |
) | |
$PlaneRow = $Seat.substring(0,7) | |
$RowCharArray = $PlaneRow.ToCharArray() | |
If ($RowCharArray[0] -eq 'F') { | |
$Pool=0..63 | |
} | |
If ($RowCharArray[0] -eq 'B') { | |
$Pool=64..127 | |
} | |
# Get the seat row | |
Foreach ($char in (1..6)){ | |
If ($RowCharArray[$char] -eq 'F') { | |
$Pool=[Math]::Ceiling($Pool[0])..[Math]::Floor(($Pool[-1]-$Pool[0]) /2 + $Pool[0] ) | |
} | |
If ($RowCharArray[$char] -eq 'B') { | |
$Pool=[Math]::Ceiling(($Pool[-1]-$Pool[0]) /2 + $Pool[0])..[Math]::Floor($Pool[-1] ) | |
} | |
} | |
# Get the seat column | |
$Column=0..7 | |
$PlaneColumn = $Seat.substring(7,3) | |
$ColumnCharArray = $PlaneColumn.ToCharArray() | |
Foreach ($char in (0..2)){ | |
If ($ColumnCharArray[$char] -eq 'L') { | |
$Column=[Math]::Ceiling($Column[0])..[Math]::Floor(($Column[-1]-$Column[0]) /2 + $Column[0] ) | |
#write-host "Set column $Column" -fore green | |
} | |
If ($ColumnCharArray[$char] -eq 'R') { | |
$Column=[Math]::Ceiling(($Column[-1]-$Column[0]) /2 + $Column[0])..[Math]::Floor($Column[-1] ) | |
#write-host "Set column $Column" -fore cyan | |
} | |
} | |
$Output = [PSCustomObject]@{ | |
Seat = $Seat | |
Pool = $Pool[0] | |
Column = $Column[0] | |
SeatID = (($Pool[0] * 8) + $Column[0]) | |
} | |
Return $Output | |
} | |
$All = Foreach ($row in $plane){ | |
Get-Seat -Seat $row | |
} | |
$All | Sort-Object SeatID | Select-Object -Last 1 | |
#------- | |
# PART 2 | |
#------- | |
$All.SeatID | Sort-Object | ForEach-Object { | |
$LastID = $ThisID | |
$ThisID = $_ | |
If ($ThisID - $LastID -ne 1 -and $LastID){ | |
Write-Host "$($ThisID -1) is not present" -fore Green | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment