Skip to content

Instantly share code, notes, and snippets.

@ArtisanByteCrafter
Last active December 23, 2020 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArtisanByteCrafter/ab6ddc33924060dd2eee277977717867 to your computer and use it in GitHub Desktop.
Save ArtisanByteCrafter/ab6ddc33924060dd2eee277977717867 to your computer and use it in GitHub Desktop.
Advent Of Code Day 5 2020
#-------
# 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