Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <vector>
- #include <numeric>
- #include <algorithm>
- #include <functional>
- using namespace std::placeholders;
- void shift(std::vector<char>& dance, int val) {
- for (int i = 0; i < val; i++) {
- char c = dance.back(); dance.pop_back();
- dance.insert(dance.begin(), c);
- }
- }
- void exchange(std::vector<char>& dance, int a, int b) {
- int tmp = dance[a];
- dance[a] = dance[b];
- dance[b] = tmp;
- }
- void partner(std::vector<char>& dance, char c1, char c2) {
- auto p1 = std::find(std::begin(dance), std::end(dance), c1);
- auto p2 = std::find(std::begin(dance), std::end(dance), c2);
- std::iter_swap(p1, p2);
- }
- void print(std::vector<char>& dance) {
- for (auto& c: dance)
- std::cout << c;
- std::cout << std::endl;
- }
- int main() {
- std::string input, token;
- std::getline(std::cin, input);
- std::stringstream ss(input);
- std::vector<char> dance(16);
- std::iota(dance.begin(), dance.end(), 'a');
- std::vector<std::function<void(std::vector<char>&)>> call;
- while (std::getline(ss, token, ',')) {
- if (token.empty()) continue;
- switch (token[0]) {
- case 's': {
- int s = std::stoi(token.substr(1))%16;
- auto x = std::bind(shift, _1, s);
- call.push_back(x);
- } break;
- case 'x': {
- auto pos = token.find('/');
- int a = std::stoi(token.substr(1, token.size()-pos));
- int b = std::stoi(token.substr(pos+1));
- auto x = std::bind(exchange, _1, a, b);
- call.push_back(x);
- } break;
- case 'p': {
- auto x = std::bind(partner, _1, token[1], token[3]);
- call.push_back(x);
- } break;
- default: break;
- }
- }
- // find cycle
- auto copy = dance;
- unsigned long long cycle = 0;
- do {
- for (auto& c: call)
- c(copy);
- cycle++;
- } while (copy != dance);
- std::cout << "Cycle: " << cycle << std::endl;
- for (unsigned long long i = 0; i < 1000000000 % cycle; i++) {
- for (auto& c: call)
- c(dance);
- }
- print(dance);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement