Categories

# My solution to Advent of Code 2020’s Day 5 challenge, in Python

Welcome to another installment in my Advent of Code 2020 series, where I present my solutions to this year’s Advent of Code challenges!

In this installment, I share my Python solution to Day 5 of Advent of Code, titled Binary Boarding.

Please be warned: If you want to try solving the challenge on your own and without any help, stop reading now! The remainder of this post will be all about my solution to both parts of the Day 5 challenge.

## The Day 5 challenge, part one

### The challenge

Here’s the text from part one of the challenge:

You board your plane only to discover a new problem: you dropped your boarding pass! You aren’t sure which seat is yours, and all of the flight attendants are busy with the flood of people that suddenly made it through passport control.

You write a quick program to use your phone’s camera to scan all of the nearby boarding passes (your puzzle input); perhaps you can find your seat through process of elimination.

Instead of zones or groups, this airline uses binary space partitioning to seat people. A seat might be specified like `FBFBBFFRLR`, where `F` means “front”, `B` means “back”, `L` means “left”, and `R` means “right”.

The first 7 characters will either be `F` or `B`; these specify exactly one of the 128 rows on the plane (numbered `0` through `127`). Each letter tells you which half of a region the given seat is in. Start with the whole list of rows; the first letter indicates whether the seat is in the front (`0` through `63`) or the back (`64` through `127`). The next letter indicates which half of that region the seat is in, and so on until you’re left with exactly one row.

For example, consider just the first seven characters of `FBFBBFFRLR`:

• Start by considering the whole range, rows `0` through `127`.
• `F` means to take the lower half, keeping rows `0` through `63`.
• `B` means to take the upper half, keeping rows `32` through `63`.
• `F` means to take the lower half, keeping rows `32` through `47`.
• `B` means to take the upper half, keeping rows `40` through `47`.
• `B` keeps rows `44` through `47`.
• `F` keeps rows `44` through `45`.
• The final `F` keeps the lower of the two, row `44`.

The last three characters will be either `L` or `R`; these specify exactly one of the 8 columns of seats on the plane (numbered `0` through `7`). The same process as above proceeds again, this time with only three steps. `L` means to keep the lower half, while `R` means to keep the upper half.

For example, consider just the last 3 characters of `FBFBBFFRLR`:

• Start by considering the whole range, columns `0` through `7`.
• `R` means to take the upper half, keeping columns `4` through `7`.
• `L` means to take the lower half, keeping columns `4` through `5`.
• The final `R` keeps the upper of the two, column `5`.

So, decoding `FBFBBFFRLR` reveals that it is the seat at row `44`, column `5`.

Every seat also has a unique seat ID: multiply the row by 8, then add the column. In this example, the seat has ID `44 * 8 + 5 = 357`.

Here are some other boarding passes:

• `BFFFBBFRRR`: row `70`, column `7`, seat ID `567`.
• `FFFBBBFRRR`: row `14`, column `7`, seat ID `119`.
• `BBFFBBFRLL`: row `102`, column `4`, seat ID `820`.

As a sanity check, look through your list of boarding passes. What is the highest seat ID on a boarding pass?

### Importing the data

Every Advent of Code participant gets their own set of data. I copied my data and went through my usual process of bringing it into Python. This involves pasting it into a triple-quoted string and assigning it to the variable `raw_input`, and then splitting it using the newline character as a delimiter, producing a list named `split_input`:

### Strategy

They dropped a hint in the title of the challenge: Binary Boarding.

They dropped a hint by saying the airlines seats people using binary space partitioning instead of zones.

They dropped a hint when they said that the row numbers range from 0 to 127, and that a seven-character sequence determined your row. Only two characters could be used in that sequence, F and B.

(Additional hint for those of you who didn’t study binary numbers: The numbers 0 through 127 can be represented using seven bits.)

They dropped a hint when they said that the seat numbers (which they called column numbers) range from 0 to 7, and that a three-character sequence determined your seat. Only two characters could be used in that sequence, L and R.

(Additional hint for those of you who didn’t study binary numbers: The numbers 0 through 7 can be represented using three bits.)

The “FB” and “LR” sequences are just binary numbers in disguise.

Solving this challenge involves:

• Converting the “FB” and “LR” strings into binary strings.
• Converting those binary strings into decimal numbers.
• Doing the math on those numbers to get the seat IDs.

### Converting the “FB” and “LR” strings into binary strings and converting those binary strings into decimal numbers

For each “FB” string, I wanted to convert the `F`s to `0`s and `B`s to `1`s. In order to do this, I used Python’s `string.maketrans()` method to build a character translation table and its `string.translate()` method to make the translation using that table.

The first line in the function uses `string.maketrans()` to define a translation table. `string.maketrans()` takes two arguments:

1. The characters to be translated.
2. The corresponding resulting characters.

In this case, I only want `F` to be translated into `0` and `B` to be translated into `1`. All other characters translated using this table will remain unchanged.

The second line does the actual translating, using the translation table as its guide.

With the function defined, I could then use it to create a list of all the rows, expressed as binary strings:

This line of code creates a new list, `binary_rows`, by taking the first seven characters of each line of input data — the “FB” string — and converting it to binary.

Here’s a sample of the result:

The next step was to convert `binary_rows` into a list of its numeric equivalents:

This line of code, creates a new list, `decimal_rows`, by converting each binary string into its decimal numeric equivalent. It does this by using the `int()` function to convert strings to integers, and the extra argument specifies that value represented in the string is in base 2, a.k.a. binary.

Here’s a sample of the result:

It was time to do the same thing with the “LR” strings. This was pretty much the same operation as with the “FB” strings.

First, an LR-to-01 converter:

Then, a list comprehension to use that converter:

This line of code creates a new list, `binary_columns`, by taking the last three characters of each line of input data — the “LR” string — and converting it to binary.

Finally, convert these numbers into decimal:

I now had two lists that I could use to do the math:

• `decimal_rows`: The “FB” sequences, converted into decimal numbers.
• `decimal_columns`: The “LR” sequences, converted into decimal numbers.

### Doing the math to get the seat IDs

As stated in the problem definition, the ID for any given seat is (its row number * 8) + (its seat number). I needed to build a list of seat IDs using `decimal_rows` and `decimal_columns`.

Here’s the code I used to do it:

The first line uses `zip()` to take two lists to make a single list filled with tuples. Each tuple has an item from the first list and a corresponding item from the second list. Here’s an example of `zip()` in action:

The second line uses the newly-created `decimal_rows_and_columns` as a basis for creating a new list of seat IDs.

Once the `seat_ids` list was created, it was a matter of using the `max()` function to get the highest ID value, which was the solution for part one. In my case, the value was 883.

## The Day 5 challenge, part two

### The challenge

Here’s the text of part two:

Ding! The “fasten seat belt” signs have turned on. Time to find your seat.

It’s a completely full flight, so your seat should be the only missing boarding pass in your list. However, there’s a catch: some of the seats at the very front and back of the plane don’t exist on this aircraft, so they’ll be missing from your list as well.

Your seat wasn’t at the very front or back, though; the seats with IDs +1 and -1 from yours will be in your list.

What is the ID of your seat?

## Strategy

It’s a full flight, so my seat should be the only one missing from the list. I could find this seat by doing the following:

• Sorting the seats in ascending order.
• Iterating through the seats, while keeping an eye out for a “gap”. That gap is my seat.

### Solution

Here’s the code I used to solve part two:

It creates a list of sorted seat IDs, which it then loops through. While looping through it, it checks to see if the ID of the seat immediately after the current one is 2 higher than the current ID. If it is, we’ve found the gap, and the ID of my seat is the ID of the current seat plus one.

In my case, the seat ID was 532. I entered that value and solved part two.

## One reply on “My solution to Advent of Code 2020’s Day 5 challenge, in Python”

Mattsays:

seats = list(seat_ids)
for seat in seats :
if (seat+2 in seats) and not(seat+1 in seats):
print(seat+1)