# 54. 火星文计算

54 54-1

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});
rl.on('line', function(line) {
    console.log(operate(line));
});
function operate(str) {
    const stack = [];
    let i = 0;
    while(i < str.length) {
        if (/\d/.test(str[i])) {
            const start = i;
            while(i < str.length && /\d/.test(str[i])) {
                i++;
            }
            const num = parseInt(str.substring(start, i));
            stack.push(num);
        } else {
            if (str[i] === '$') {
                const y = stack.pop();
                i++;
                const start = i;
                while(i<str.length && /\d/.test(str[i])) {
                    i++;
                }
                const x = parseInt(str.substring(start, i));
                stack.push(3*y + x + 2);
            } else if (str[i] === '#') {
                i++;
            }
        }
    }
    const reverse = [];
    while(stack.length > 0) {
        reverse.push(stack.pop());
    }
    let result = reverse.pop();
    while(reverse.length > 0) {
        const x = reverse.pop();
        result = 2*result + 3*x + 4;
    }
    return result;
}
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