-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (54 loc) · 2.8 KB
/
index.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
/*
* 👉 : moves the memory pointer to the next cell
* 👈 : moves the memory pointer to the previous cell
* 👆 : increment the memory cell at the current position
* 👇 : decreases the memory cell at the current position.
* 🤜 : if the memory cell at the current position is 0, jump just after the corresponding 🤛
* 🤛 : if the memory cell at the current position is not 0, jump just after the corresponding 🤜
* 👊 : Display the current character represented by the ASCII code defined by the current position.
*/
const MIN_CELL = 0;
const MAX_CELL = 255;
const processLoops = (AOS) => {
let stack = [], loopPairs = [];
for (let i = 0; i < AOS.length; i++) {
if (AOS[i] === '🤜') {
stack.push(i);
} else if (AOS[i] === '🤛') {
let openLoop = stack.pop();
loopPairs[openLoop] = i;
loopPairs[i] = openLoop;
}
}
return loopPairs;
}
const translate = (input) => {
const AOS = Array.from(input);
let loopPairs = processLoops(AOS); // Generamos estructura con pares de índices para loops
let pointer = 0;
let memory = [0];
let output = '';
for (let i = 0; i < AOS.length; i++) {
switch(AOS[i]) {
case '👉': pointer++; memory[pointer] ??= 0; break;
case '👈': pointer--; memory[pointer] ??= 0; break;
case '👆': memory[pointer] = memory[pointer]+1 > MAX_CELL ? MIN_CELL : memory[pointer]+1; break;
case '👇': memory[pointer] = memory[pointer]-1 < MIN_CELL ? MAX_CELL : memory[pointer]-1; break;
case '🤜':
if (memory[pointer] === 0) {
i = loopPairs[i];
}
break;
case '🤛':
if (memory[pointer] !== 0) {
i = loopPairs[i];
}
break;
case '👊': output += String.fromCharCode(memory[pointer]); break;
}
// console.log(`${AOS[i]} ; ${i} ; ${pointer} ; ${output} ; ${memory}`);
}
console.log(output);
}
translate('👇🤜👇👇👇👇👇👇👇👉👆👈🤛👉👇👊👇🤜👇👉👆👆👆👆👆👈🤛👉👆👆👊👆👆👆👆👆👆👆👊👊👆👆👆👊');
translate('👉👆👆👆👆👆👆👆👆🤜👇👈👆👆👆👆👆👆👆👆👆👉🤛👈👊👉👉👆👉👇🤜👆🤛👆👆👉👆👆👉👆👆👆🤜👉🤜👇👉👆👆👆👈👈👆👆👆👉🤛👈👈🤛👉👇👇👇👇👇👊👉👇👉👆👆👆👊👊👆👆👆👊👉👇👊👈👈👆🤜👉🤜👆👉👆🤛👉👉🤛👈👇👇👇👇👇👇👇👇👇👇👇👇👇👇👊👉👉👊👆👆👆👊👇👇👇👇👇👇👊👇👇👇👇👇👇👇👇👊👉👆👊👉👆👊');