-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar-escape.js
53 lines (48 loc) · 1.46 KB
/
car-escape.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
const STAIRS = 1;
const START = 2;
function findStartingFloor(carpark) {
return carpark.findIndex((floor) => floor.indexOf(START) >= 0);
}
function getStepsWalkedToStaircase(floor, floorPosition) {
return floor.indexOf(STAIRS) - floorPosition;
}
export default function escape(carpark) {
const directions = [];
let currentFloorLevel = findStartingFloor(carpark);
let currentFloorPosition = carpark[currentFloorLevel].indexOf(START);
while (currentFloorLevel < carpark.length - 1) {
const numSteps = getStepsWalkedToStaircase(
carpark[currentFloorLevel],
currentFloorPosition
);
// move left
if (numSteps < 0) {
directions.push(`L${Math.abs(numSteps)}`);
directions.push('D1');
}
// move right
if (numSteps > 0) {
directions.push(`R${Math.abs(numSteps)}`);
directions.push('D1');
}
// move down
if (numSteps === 0) {
const lastDirectionAdded = directions[directions.length - 1];
if (lastDirectionAdded[0] === 'D') {
directions.pop();
directions.push(`D${+lastDirectionAdded[1] + 1}`);
} else {
directions.push('D1');
}
}
// adjust current floor position and go to next floor level
currentFloorPosition += numSteps;
currentFloorLevel++;
}
const stairPos = carpark[currentFloorLevel].length - 1;
const numSteps = stairPos - currentFloorPosition;
if (numSteps > 0) {
directions.push(`R${numSteps}`);
}
return directions;
}