Mastodon

2020 Advent Of Code: Day 5

Powershell Dec 23, 2020

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
$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
}


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[0] and $Column[0] , 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

Tags