#include #include #include #include #include #define S first #define I second #define pb push_back using namespace std; vector> buffer; vector heatmap; int counter = 0; int state[256]; int findClosingBracket(int pos) { int b = 1; while (++pos < buffer.size() && b != 0) { if (buffer[pos].S == "(") b++; if (buffer[pos].S == ")") b--; } return --pos; } int solve(int start, int rep) { int pos; for (int i = 0; i < rep; i++) { pos = start; while (pos < buffer.size() && buffer[pos].S != ")") { counter++; heatmap[buffer[pos].I]++; if (buffer[pos].S == "CLEAR") { state[buffer[pos+1].S[0]] = 0; pos += 2; } else if (buffer[pos].S == "INCREASE") { state[buffer[pos+1].S[0]]++; pos += 2; } else if (buffer[pos].S == "PRINT") { pos += 2; } else if (buffer[pos].S == "REPEAT") { pos = solve(pos + 4, state[buffer[pos+1].S[0]]); } else { cerr << "Error. Line: " << buffer[pos].I << " Token: " << buffer[pos].S << endl; exit(1); } } } if (rep == 0) pos = findClosingBracket(start); return pos + 1; } int main() { int l = 1; string line; while (getline(cin, line)) { string processed = ""; for (char & c : line) { if (c == '#') break; processed += c; } istringstream iss(processed); string token; while (iss >> token) buffer.pb({token, l}); l++; } int y = 0; for (auto p : buffer) if (p.first == "CLEAR" || p.first == "INCREASE" || p.first == "REPEAT" || p.first == "PRINT") y++; cout << endl << "Number of commands: " << y << endl; for (int i = 0; i < l; i++) heatmap.pb(0); int r = 0; for (int i = 1; i < 1000; i++) { for (int & v : state) v = 0; state['X'] = i; solve(0, 1); if (counter <= 60000000) r = i; } cout << endl << "Instruction limit reached: " << r << endl; cout << endl << "Total instruction count: " << counter << endl; float s = 0; for (float & f : heatmap) s += f; for (float & f : heatmap) f = 1000 * f / s; vector>> areas; areas.pb({"base-2",{1, 147}}); areas.pb({"x-not-1",{168, 223}}); areas.pb({"z-180",{244, 271}}); areas.pb({"3x+1",{315, 805}}); areas.pb({"print-3x+1",{827, 832}}); areas.pb({"x/2",{855, 927}}); areas.pb({"base-10",{947, 1401}}); areas.pb({"x-not-1",{1421, 1477}}); cout << endl; for (auto & a : areas) { float s = 0; for (int i = a.second.first; i <= a.second.second; i++) s += heatmap[i]; cout << setw(11) << a.first << setw(5) << (int)s << endl; } cout << endl; for (int i = 0; i < heatmap.size(); i++) if (heatmap[i] >= 1) cout << setw(4) << i << setw(7) << (int)heatmap[i] << endl; }