Advent of Code — Day 5 — Javascript
By: Vincent Yang
Tags: Advent Of Code 2020, Flatiron School, Algorithms, JavaScript, Advent Of Code

For Day 5, you are asked to use parse through a string of 10 characters which point to a specific seat on a plane. The airplane has 128 rows and 8 seats per row. The first seven characters, made up of F’s and B’s, identify the row of the seat and the last three characters, made up of L’s and R’s identify the seat in the row. Each character halves the possible seats and tells whether to take the first half or the second half. Given the string:

FBFBBFFRLR

The first F refers to the first half of the 128 seats which reduces the number of rows from 0 through 127 to 0 through 63. The second character, B, reduces the number of rows by half again and takes the latter half of the count from 0 through 63 to 32 through 63. The process continues as shown below:

FBFBBFF

1. F = 0 to 63

2. B = 32 to 63

3. F = 32 to 47

4. B = 40 to 47

5. B = 44 to 47

6. F = 44 to 45

7. F = 44

The last three characters parse through eight seats or 0 to 7. As we read left to right, L means to take the lower bound and R means to take the upper bound.

RLR

1. R = 4 to 7

2. L = 4 to 5

3. R = 5

The correct seat for this sequence is row 44, seat 5 from the left.

The return value that the problem asks for is the seat ID which is calculated with the row value times 8 plus seat value or 44 * 8 + 5 = 357.

Let’s look at the sample data and their respective answers:

BFFFBBFRRR : row 70 , column 7 , seat ID 567

FFFBBBFRRR : row 14 , column 7 , seat ID 119

BBFFBBFRLL : row 102 , column 4 , seat ID 820

For the data that is provided, you need to return the max of all the seat IDs. For the above data, 820 would be the answer.

function day5(input){

let arr = [] //Array to store all seat IDs

const directions = input.split('

')

for(let i = 0; i < directions.length; i++){

let front = 0;

let back = 127;

let left = 0;

let right = 7;

}

}

This basic setup allows you to calculate the seat by reassigning the upper and lower bounds.

function day5(input){

let arr = [] //Array to store all seat IDs

const directions = input.split('

')

for(let i = 0; i < directions.length; i++){

let front = 0;

let back = 127;

let left = 0;

let right = 7;

for(let j = 0; j < 7; j++){

directions[i][j] === 'F' ? back = Math.floor((back + front) / 2)} : null

directions[i][j] === 'B' ? front = Math.ceil((back + front) / 2)

}

}

}

For each set of directions, we are resetting all of the bounds and depending on the character that appears, we are taking the lower or upper bound. The tricky part is reassigning the bounds to the floor or ceiling of the reassigned bounds. If we take a look at the example that was provided:

FBFBBFF 0 to 127 1. F

Center = (0 + 127) / 2 = 63.5

Front / Lower Bound = 0 to 63.5

Back / Upper Bound = 63.5 to 127

For F, we are rounding down so 63.5 => 63

F (Front Bound) = 0 to 63 2. B

Center = (0 + 63) / 2 = 31.5

Front / Lower Bound = 0 to 31.5

Back / Upper Bound = 31.5 to 63

For B, we are rounding up so 31.5 => 32

B (Back Bound) = 32 to 63 3. F

Center = (32 + 63) / 2 = 47.5

Front / Lower Bound = 32 to 47.5

Back / Upper Bound = 47.5 to 63

For F, we are rounding down so 47.5 => 47

F (Front Bound) = 32 to 47 4. B

Center = (32 + 47) / 2 = 39.5

Front / Lower Bound = 32 to 39.5

Back / Upper Bound = 39.5 to 47

For B, we are rounding up so 39.5 => 40

B (Back Bound) = 40 to 47 5. B

Center = (40 + 47) / 2 = 43.5

Front / Lower Bound = 40 to 43.5

Back / Upper Bound = 43.5 to 47

For B, we are rounding up so 43.5 => 44

B (Back Bound) = 44 to 47 6. F

Center = (44 + 47) / 2 = 45.5

Front / Lower Bound = 44 to 45.5

Back / Upper Bound = 45.5 to 47

For F, we are rounding down so 45.5 => 45

F (Front Bound) = 44 to 45 7. F = 44

After doing the same calculations on the seats, we should come up with something like this:

function day5(input){

let arr = [] //Array to store all seat IDs

const directions = input.split('

')

for(let i = 0; i < directions.length; i++){

let front = 0;

let back = 127;

let left = 0;

let right = 7;

for(let j = 0; j < 7; j++){

directions[i][j] === 'F' ? back = Math.floor((back + front) / 2)} : null

directions[i][j] === 'B' ? front = Math.ceil((back + front) / 2)

}

for(let j = 7; j < 10; j++){

directions[i][j] === 'L' ? right = Math.floor((right + left) / 2)

directions[i][j] === 'R' ? left = Math.ceil((right + left) / 2)

}

}

}

Once we are able to get the proper row and seat values for each line, we will need to calculate the seat IDs and store them into the array that we created at the start.