-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.js
More file actions
40 lines (31 loc) · 869 Bytes
/
Copy pathsolution.js
File metadata and controls
40 lines (31 loc) · 869 Bytes
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
const decorateTree = base => {
const ornamentFormula = {
BB: 'B',
BP: 'R',
BR: 'P',
PB: 'R',
PP: 'P',
PR: 'B',
RB: 'P',
RP: 'B',
RR: 'R'
};
const splittedBase = base.split(' ');
const addOrnament = (accumulator, currentValue, currentIndex, original) => {
const nextValue = original[currentIndex + 1];
if (nextValue !== undefined) {
accumulator.push(ornamentFormula[currentValue + nextValue]);
}
return accumulator;
};
const createTree = (accumulator, _currentValue, currentIndex) => {
const nextLine = accumulator[currentIndex].reduce(addOrnament, []);
accumulator.push(nextLine);
return accumulator;
};
return [...Array(splittedBase.length - 1)]
.reduce(createTree, [splittedBase])
.reverse()
.map(ornaments => ornaments.join(' '));
};
export { decorateTree };