-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathplane.js
42 lines (36 loc) · 891 Bytes
/
plane.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function binarySpacePartitionToSeatId(code) {
let code_arr = code.split('');
let low_row = 0;
let high_row = 127;
// First 7 char
let rows = code_arr.slice(0, 7);
let last_row_command = rows.pop();
// Last 3 chars
let cols = code_arr.slice(7);
let last_col_command = cols.pop();
for (let dir of rows) {
// Seats `0 - 127` is 128 seats
let seats = high_row - low_row + 1;
let half = seats / 2;
if (dir === 'F') {
high_row -= half;
} else {
low_row += half;
}
}
let row = last_row_command === 'F' ? low_row : high_row;
let low_col = 0;
let high_col = 7;
for (let dir of cols) {
let seats = high_col - low_col + 1;
let half = seats / 2;
if (dir === 'L') {
high_col -= half;
} else {
low_col += half;
}
}
let col = last_col_command === 'L' ? low_col : high_col;
return row * 8 + col;
}
module.exports = { binarySpacePartitionToSeatId };