Binary Boarding
Tags: adventofcode, algorithms, computerscience, programming
Published: 2022-04-13 00:01:24

## [Advent of Code 2020 Day 5](https://adventofcode.com/2020/day/5)

## [Try the simulator!](https://aocbinaryboarding.rmion.repl.co/)
![Simulation of both parts of algorithm](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yrwjs52qr7v8ytkg1cfm.gif)

## Task: Solve for X where...
Part 1:
```
X = the highest boarding pass ID
```
Part 2:
```
X = my boarding pass ID
```

### Example input
```
FBFBBFFRLR
BFFFBBFRRR
FFFBBBFRRR
BBFFBBFRLL
```
It represents:
- A list of encrypted boarding pass IDs

## Part 1
1. Using the title as a hint
2. Writing a working algorithm
3. Building the simulator

### Using the title as a hint
- The title is `Binary` Boarding
- The instructions further suggest going one of two directions based on the character encountered
- The solution seems clear

Given this boarding pass
```
FBFBBFFRLR
```

Splitting `FB` from `RL`
```
FBFBBFF RLR
```

Replacing `F`s and `R`s with `1`s, `B`s and `L`s with `0`s
```
1010011 101
```

Parsing each one as a decimal
```
44       5
```

Generating the ID
```
44 * 8 + 5 = 357
```

### Writing a working algorithm
```
Split the input at each new-line character into an array of strings

For each string
  Change the string according to the following instructions:
    Split the string into groups containing exclusively FBs and RLs
    Replace FBs with 1 and RLs with 0
    Parse the modified string as a decimal
    Calculate the sum of:
      The product of the first decimal and 8
      And the second decimal
  Return the sum

Return the highest number in the modified list of decimals
```

Here's a visualization of my algorithm for Part 1
![Visualization of my algorithm for Part 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c62sa1g8razgm7d9sa6g.gif)

### Building the simulator
- Since these are boarding pass IDs, I wanted to simulate the population of a plane's seating chart
- By the end of running Part 1, there should be one seat left unfilled: that's my seat!

## Part 2
1. Writing a slightly modified, working algorithm
2. Finishing the simulator

### Writing a slightly modified, working algorithm
- I still need to generate the list of boarding pass IDs
- But now I need to sort them in ascending order
- And I need to find the only gap: where the number increments by 2 instead of 1

```
Split the input at each new-line character into an array of strings

For each string
  Change the string according to the following instructions:
    Split the string into groups containing exclusively FBs and RLs
    Replace FBs with 1 and RLs with 0
    Parse the modified string as a decimal
    Calculate the sum of:
      The product of the first decimal and 8
      And the second decimal
  Return the sum

Sort the list of decimals in ascending order

Filter the list to exclude all but one number:
  Find the number where the number at the very next location is not equal to this number plus one

Since the number found is the seat next to mine
  Return that number plus one
```

Here's a visualization of my algorithm for Part 2
![Visualization of my algorithm for Part 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rgl5bvldcj52ih3qlimj.gif)

### Finishing the simulator
As foreshadowed above, I wanted Part 2 to reveal the seat that in Part 1 was the only gap

[Try the simulator!](https://aocbinaryboarding.rmion.repl.co/)
![Simulation of both parts of algorithm](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yrwjs52qr7v8ytkg1cfm.gif)

This puzzle felt easy.

That made me feel great about my progress.

Bring on the next one!