-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (42 loc) · 1.11 KB
/
index.js
File metadata and controls
50 lines (42 loc) · 1.11 KB
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
function decorateTree(base) {
let combinations = {
'PP': 'P', 'RR': 'R', 'BB': 'B',
'PB': 'R', 'BP': 'R', 'RP': 'B',
'PR': 'B', 'BR': 'P', 'RB': 'P'
}
const result = [ base ]
let baseToArray = base.split(' ')
let list = new Array(baseToArray.length - 1).fill('')
list.map(_ => {
baseToArray = [ ...baseToArray ].splice(1).map((val, index) => {
const key = baseToArray[index] + val
const resultCombination = combinations[key]
return resultCombination
})
result.push(baseToArray.join(' '))
})
return result.reverse()
}
function decorateTreeAlt(base) {
const dict = {
"PP": "P",
"BB": "B",
"RR": "R",
"BP": "R",
"PB": "R",
"BR": "P",
"RB": "P",
"PR": "B",
"RP": "B"
}
base = base.split(" ")
let list = new Array(base.length).fill(base)
return list.reduce((total, x) =>
total.concat(
[ new Array(total.at(-1).length - 1).fill("-").map((_, i) => {
return dict[total.at(-1).slice(i, i + 2).join("")]
}) ]
), [base]
).slice(0, base.length).map(x => x.join(" ")).reverse()
}
module.exports = decorateTree