Last active
January 1, 2023 06:45
-
-
Save MeherajUlMahmmud/a3d49605f6b7dcace446903f6128095e to your computer and use it in GitHub Desktop.
Airplane Seatplan
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var seatplan = [ | |
[3, 4], | |
[4, 5], | |
[2, 3], | |
[3, 4], | |
]; | |
// var seatplan = [ | |
// [3, 2], | |
// [4, 3], | |
// [2, 3], | |
// [3, 4], | |
// ]; | |
passengers = 36; | |
var complete_seatplan = []; | |
for (var i = 0; i < seatplan.length; i++) { | |
var row = seatplan[i]; | |
var matrix = []; | |
for (var j = 0; j < row[1]; j++) { | |
var row_mat = []; | |
for (var k = 0; k < row[0]; k++) { | |
row_mat.push(0); | |
} | |
matrix.push(row_mat); | |
} | |
complete_seatplan.push(matrix); | |
} | |
// corners columns | |
var corners = [0, complete_seatplan.length - 1]; | |
var seat_count = 0; | |
var total_iterations = 0; | |
var i = 0; | |
var j = 0; | |
var k = 0; | |
var undefined_count = 0; | |
var passenger_count = 0; | |
while (total_iterations < 3) { | |
if (i >= complete_seatplan.length) { | |
i = 0; | |
j++; | |
} | |
var row_matrix = complete_seatplan[i][j]; | |
if (row_matrix === undefined) { | |
i++; | |
undefined_count++; | |
if (undefined_count === complete_seatplan.length) { | |
// if all rows are undefined, then move to next iteration of seat assignment (Aisle, Window, Middle) | |
undefined_count = 0; | |
total_iterations++; | |
i = 0; | |
j = 0; | |
k = 0; | |
} | |
} else { | |
undefined_count = 0; | |
} | |
if (corners.includes(i) && i === corners[0]) { | |
if (row_matrix !== undefined) { | |
if (total_iterations === 0) { | |
// total_iterations = 0 means Aisle seats | |
seat_count = seat_count + 1; | |
// if assigned all passengers, then break | |
if (seat_count === passengers) break; | |
row_matrix[row_matrix.length - 1] = `A${seat_count}`; | |
i++; // move to next column | |
} else if (total_iterations === 1) { | |
// total_iterations = 1 means Window seats | |
seat_count = seat_count + 1; | |
// if assigned all passengers, then break | |
if (seat_count === passengers) break; | |
row_matrix[0] = `W${seat_count}`; | |
i++; // move to next column | |
} else if (total_iterations === 2) { | |
// total_iterations = 2 means Middle seats | |
for (var l = 1; l < row_matrix.length - 1; l++) { | |
// iterate through middle seats (excluding aisle and window seats) | |
seat_count = seat_count + 1; | |
// if assigned all passengers, then break | |
if (seat_count === passengers) break; | |
row_matrix[l] = `M${seat_count}`; | |
} | |
i++; // move to next column | |
} | |
} | |
} else if (corners.includes(i) && i === corners[1]) { | |
if (row_matrix !== undefined) { | |
if (total_iterations === 0) { | |
seat_count = seat_count + 1; | |
if (seat_count > passengers) break; | |
row_matrix[0] = `A${seat_count}`; | |
i = 0; | |
j++; | |
} else if (total_iterations === 1) { | |
seat_count = seat_count + 1; | |
if (seat_count > passengers) break; | |
row_matrix[row_matrix.length - 1] = `W${seat_count}`; | |
i = 0; | |
j++; | |
} else if (total_iterations === 2) { | |
for (var l = 1; l < row_matrix.length - 1; l++) { | |
seat_count = seat_count + 1; | |
if (seat_count > passengers) break; | |
row_matrix[l] = `M${seat_count}`; | |
} | |
i = 0; | |
j++; | |
} | |
} | |
} else { | |
if (row_matrix !== undefined) { | |
if (total_iterations === 0) { | |
seat_count = seat_count + 1; | |
if (seat_count > passengers) break; | |
row_matrix[k] = `A${seat_count}`; | |
seat_count = seat_count + 1; | |
if (seat_count > passengers) break; | |
row_matrix[row_matrix.length - 1] = `A${seat_count}`; | |
i++; | |
} else if (total_iterations === 1) { | |
i++; | |
} else if (total_iterations === 2) { | |
for (var l = 1; l < row_matrix.length - 1; l++) { | |
seat_count = seat_count + 1; | |
if (seat_count > passengers) break; | |
row_matrix[l] = `M${seat_count}`; | |
} | |
i++; | |
} | |
} | |
} | |
} | |
for (var i = 0; i < complete_seatplan.length; i++) { | |
var row = complete_seatplan[i]; | |
for (var j = 0; j < row.length; j++) { | |
var row_mat = row[j]; | |
console.log(row_mat); | |
} | |
console.log(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment