-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisRobotBack.js
60 lines (48 loc) · 1.09 KB
/
isRobotBack.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
export function isRobotBack(moves) {
const modifiedMoves = {
'*': 1,
L: 'R',
R: 'L',
U: 'D',
D: 'U',
}
const movesDictionary = {
R: 0,
L: 0,
U: 1,
D: 1,
}
const ejes = [0, 0]
const steps = moves.split('')
const stepsHistory = new Set()
let diff = true
for (let i = 0; i < steps.length; i++) {
const step = steps[i]
let stepNext = steps[i + 1]
stepsHistory.add(step)
if (step === '*') {
ejes[movesDictionary[stepNext]] = ejes[movesDictionary[stepNext]] + 1
}
if (step === '!') {
steps[i + 1] = modifiedMoves[stepNext]
}
if (step === '?') {
const nextStep = steps[i + 1]
if (stepsHistory.has(nextStep)) {
diff = false
} else {
diff = true
}
}
if (diff) {
if (step === 'R' || step === 'U') {
ejes[movesDictionary[step]] += 1
} else if (step === 'L' || step === 'D') {
ejes[movesDictionary[step]] -= 1
}
}
}
return ejes.some(eje => eje > 0 || eje < 0) ? ejes : true
}
const result = isRobotBack('*RU')
console.log(result)