language: C++11 (gcc-4.7.2)
date: 215 days 20 hours ago
link:
visibility: public
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
#include <unordered_map>
#include <algorithm>
#include <iostream>
#include <string>
#include <stack>
 
template<typename BiIter> void run(BiIter first, BiIter last) {
    std::size_t dp=0;
    std::unordered_map<std::size_t, int> data;
    std::stack<BiIter> jmps;
 
    for(auto it=first; it!=last; ++it) {
        switch(*it) {
        case '>': ++dp;       break;
        case '<': --dp;       break;
        case '+': ++data[dp]; break;
        case '-': --data[dp]; break;
        case '.': 
            std::cout << static_cast<char>(data[dp]);
            break;
        case ',': {
            char c;
            std::cin >> c;
            data[dp]=c;
            break;
        }
        case '[': 
            if(data[dp]==0) 
               it=std::find(it, last, ']');
            else 
               jmps.push(it);
            break;
        case ']': 
            it=jmps.top()-1;
            jmps.pop();
            break;
        }   
    }   
}   
void run(const std::string& code) { run(code.begin(), code.end()); }
int main() {
    std::string program="++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.";
    run(program);
}