Skip to content

Instantly share code, notes, and snippets.

@paulll

paulll/qset.cc Secret

Created June 10, 2020 21:53
Show Gist options
  • Save paulll/0d7b59bd609a2b937e4bd075d7f96217 to your computer and use it in GitHub Desktop.
Save paulll/0d7b59bd609a2b937e4bd075d7f96217 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <vector>
#include <iterator>
#include <sstream>
using namespace std;
typedef multiset<string> qset;
qset split_to_qset(string line) {
stringstream ss(line);
istream_iterator<string> begin(ss);
istream_iterator<string> end;
vector<string> vstrings(begin, end);
qset q(vstrings.begin(), vstrings.end());
return q;
}
void display_current(qset state) {
set<string> names(state.begin(), state.end());
for (const auto name: names) {
cout << name << "=" << state.count(name) << " ";
}
cout << endl;
}
int main(int argc, char const *argv[]) {
qset state;
vector<pair<qset, qset>> program;
for (int i = 0; i < argc; ++i) {
int val = atoi(argv[i]);
for (int a = 0; a < val; ++a) {
state.insert("i" + to_string(i));
}
}
string line;
while (getline(cin, line)) {
size_t pos = line.find_first_of('|');
if (pos == string::npos) continue;
string first_part = line.substr(0, pos), second_part = line.substr(pos+1);
program.emplace_back(pair<qset,qset>(split_to_qset(first_part), split_to_qset(second_part)));
}
size_t current_line = 0;
size_t limit = 100000;
while (current_line < program.size() && --limit) {
if (includes(state.begin(), state.end(), program[current_line].second.begin(),program[current_line].second.end())) {
qset diff;
set_difference(state.begin(), state.end(), program[current_line].second.begin(), program[current_line].second.end(), inserter(diff, diff.begin()));
state.swap(diff);
state.insert(program[current_line].first.begin(), program[current_line].first.end());
current_line = 0;
display_current(state);
} else {
current_line++;
}
}
if (!limit) {
cerr << "Execution steps limit exceeded" << endl;
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment