Skip to content

Instantly share code, notes, and snippets.

@tjwei
Created June 17, 2015 03:37
Show Gist options
  • Save tjwei/ffc1b5cad0be74e542e1 to your computer and use it in GitHub Desktop.
Save tjwei/ffc1b5cad0be74e542e1 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <map>
#include <fstream>
int main(int argc, char **argv) {
std::map<int,unsigned char> cells;
std::ifstream is(argv[1], std::ifstream::binary);
auto loop = [&](int s, char x, char y, int cnt=0) -> bool {
while(cnt >= 0 && is.seekg(s, is.cur))
[&](int d){cnt += (d == x) - (d == y);}(is.get());
};
for(int p=0;!is.eof();) {
auto c = is.get();
p += (c == '>') - (c == '<');
cells[p] += (c == '+') - (c == '-');
c=='.' && std::cout << cells[p];
c==',' && (cells[p] = std::cin.get());
c=='[' && !cells[p] && loop(0, '[', ']');
c==']' && cells[p] && loop(-2, ']', '[');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment