-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
116 lines (104 loc) · 2.77 KB
/
controller.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
function constructSeats(input, seats) {
for (let i = 0; i < input.length; i++) {
const rows = input[i][1]
const cols = input[i][0]
let seat = []
for (let j = 0; j < rows; j++) {
seat.push(Array(cols).fill())
}
seats.push(seat)
}
}
function determineAisleSeats(seats) {
for (let i = 0; i < seats.length; i++) {
for (let j = 0; j < seats[i].length; j++) {
const childArray = seats[i][j]
if (i < 1) {
childArray[childArray.length - 1] = "A"
} else if (childArray.length === 2 || i !== seats[i].length - 1) {
childArray[0] = "A"
childArray[childArray.length - 1] = "A"
} else {
childArray[0] = "A"
}
}
}
}
function determineWindowSeats(seats) {
const firstChildArray = seats[0]
const lastChildArray = seats[seats.length - 1]
for (let i = 0; i < firstChildArray.length; i++)
firstChildArray[i][0] = "W"
for (let i = 0; i < lastChildArray.length; i++)
lastChildArray[i][(lastChildArray[i].length) - 1] = "W";
}
function determineMiddleSeats(seats) {
for (let i = 0; i < seats.length; i++) {
for (let j = 0; j < seats[i].length; j++) {
const childArray = seats[i][j]
if (childArray.length > 2) {
childArray.map((_, idx) => {
if (idx > 0 && idx < childArray.length - 1) {
childArray[idx] = "M"
}
})
}
}
}
}
function replaceAndSwapCharWithNumber(val, counter, seats, colSize, rowSize, passengerCount) {
for (let i = 0; i < colSize; i++) {
for (let j = 0; j < rowSize; j++) {
if (seats[j] == null || seats[j][i] == null) continue
for (let k = 0; k < seats[j][i].length; k++) {
if (seats[j] != null && seats[j][i] != null && seats[j][i][k] === val) {
if (counter > passengerCount) {
seats[j][i][k] = "XX"
counter++
continue
}
seats[j][i][k] = counter
counter++
}
}
}
}
return { seats, counter }
}
function createSpace(rowSize) {
let idx = 0
let spaceStr = ""
while (idx < rowSize) {
spaceStr += " "
idx++
}
return spaceStr
}
function printValues(rowSize, colSize, seats) {
let stringRes = ""
for (let i = 0; i < colSize; i++) {
for (let j = 0; j < rowSize; j++) {
if (seats[j]) {
stringRes += "[ "
for (let k = 0; k < (seats[j][i] && seats[j][i].length); k++) {
stringRes += (`${seats[j][i][k]} `)
}
if (!seats[j][i]) {
stringRes += createSpace(rowSize)
}
stringRes += "]"
}
stringRes += ""
}
stringRes += "\n"
}
return stringRes
}
module.exports = {
constructSeats,
determineAisleSeats,
determineMiddleSeats,
determineWindowSeats,
printValues,
replaceAndSwapCharWithNumber,
}