-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproblem_269.js
49 lines (45 loc) Β· 1.2 KB
/
problem_269.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
/**
* Changes orientation of dominos when they fall
* @param {string} dominos
* @return {string}
*/
function changeOrientation(dominos) {
let changes = 0;
const newDominos = [...dominos];
for (let i = 0; i < dominos.length; i++) {
if (
dominos[i] === 'L' &&
i > 0 &&
dominos[i - 1] === '.' &&
dominos[i - 2] !== 'R'
) {
newDominos[i - 1] = 'L';
changes += 1;
} else if (
dominos[i] === 'R' &&
i < dominos.length - 1 &&
dominos[i + 1] === '.' &&
dominos[i + 2] !== 'L'
) {
newDominos[i + 1] = 'R';
changes += 1;
}
}
return changes > 0 ? changeOrientation(newDominos) : dominos;
}
/**
* Determines orientation of dominos when they stop falling
* @param {string} dominos
* @return {string}
*/
function getOrientation(dominos) {
if (!dominos) return '';
const dominoArr = [...dominos];
return changeOrientation(dominoArr).join('');
}
console.log(getOrientation('')); // ''
console.log(getOrientation('.')); // '.'
console.log(getOrientation('L')); // 'L'
console.log(getOrientation('R')); // 'R'
console.log(getOrientation('.L.R....L')); // 'LL.RRRLLL'
console.log(getOrientation('..R...L.L')); // '..RR.LLLL'