# 2020 Advent Of Code: Day 5

This year, I'm choosing a few of the Advent Of Code challenges I've particularly enjoyed. The first one will be for Day 5, where' we're challenged to find information regarding a seat on a plane.

### Part One

Part One challenges us to find the highest seating ID, given a list of seat assignments. I won't go into all the rules, as you can find them on the Advent Of Code page for day 5, but here is my Powershell solution: (If scrolling is a pain, I've included the code in a Github gist as well)

``````#-------
# PART 1
#-------
# input file

Function Get-Seat {
[cmdletbinding()]
param(
[Parameter(Mandatory)]
[string] \$Seat
)

\$PlaneRow = \$Seat.substring(0,7)
\$RowCharArray = \$PlaneRow.ToCharArray()

If (\$RowCharArray -eq 'F') {
\$Pool=0..63
}
If (\$RowCharArray -eq 'B') {
\$Pool=64..127
}

# Get the seat row
Foreach (\$char in (1..6)){
If (\$RowCharArray[\$char] -eq 'F') {
\$Pool=[Math]::Ceiling(\$Pool)..[Math]::Floor((\$Pool[-1]-\$Pool) /2 + \$Pool )
}
If (\$RowCharArray[\$char] -eq 'B') {
\$Pool=[Math]::Ceiling((\$Pool[-1]-\$Pool) /2 + \$Pool)..[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)..[Math]::Floor((\$Column[-1]-\$Column) /2 + \$Column )
#write-host "Set column \$Column" -fore green
}
If (\$ColumnCharArray[\$char] -eq 'R') {
\$Column=[Math]::Ceiling((\$Column[-1]-\$Column) /2 + \$Column)..[Math]::Floor(\$Column[-1] )
#write-host "Set column \$Column" -fore cyan
}
}

\$Output = [PSCustomObject]@{
Seat   = \$Seat
Pool   = \$Pool
Column = \$Column
SeatID = ((\$Pool * 8) + \$Column)
}
Return \$Output
}

\$All = Foreach (\$row in \$plane){
Get-Seat -Seat \$row
}

PS> \$All | Sort-Object SeatID | Select-Object -Last 1

Seat       Pool Column SeatID
----       ---- ------ ------
BBFFBFBRLR  101      5    813
``````

What I've done is create a function that takes a `Seat` parameter, and will iterate over the character array of the first 7 characters to arrive at a seat row, then again over the last 3 characters to arrive at a column. The output is a PSCustomObject with the `seat` (input), it's `row`, `column`, and `SeatID`.

To get the row, I used the `[Math]::Floor()` and `[Math]::Ceiling()` classes to iterate through the character array of the first 7 values of a seat, each time dividing my array of available rows by 2 (essentially splitting it in half each time, keeping either the upper or lower half of the preceding operation). After doing this for all 7 rows, I was left with an array of 1 number, which was my row.

For the column, i took the same approach, except with the last 3 values of the seat.

In the output object, in order to get a single number instead of an array of one number (for example `5` instead of `{5}`), I made the value of my custom object parameter `\$Pool` and `\$Column` , as they would only have a single object in their array at that point. This is necessary, as arrays do not contain arithmetic operators, so calculating the SeatID would not have been possible.

### Part Two

Part two essentially needed to find which seat ID is missing in our list of IDs, which are already ordered sequentially. To do so, I chose an approach that subtracts the current item in a foreach-object loop from the previous one, and if the remainder is not 1, they were not sequential. In my case, ID `612` was missing, so that must be my ID.

``````#-------
# 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
}
}

PS> 612 is not present

``````