Advertisement
GeoffreyYeung

Idle tube questions

Oct 14th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. unsigned SIZE = 10;
  6.  
  7. template <typename T>
  8. void printVector(std::vector<T> v) {
  9.     for (auto i : v)
  10.         std::cout << i << ' ';
  11.     std::cout << '\n';
  12.     return;
  13. }
  14.  
  15. bool imply(bool left, bool right) {
  16.     if (left && !right)
  17.         return false;
  18.     return true;
  19. }
  20.  
  21. bool applyLogic(std::vector<bool> values, std::vector<unsigned> order, bool verbose = false) {
  22.     if (verbose)
  23.         printVector(order);
  24.     if (values.size() != order.size() + 1) {
  25.         std::cerr << "different value vector size and order vector size" << std::endl;
  26.         return false;
  27.     }
  28.     static unsigned s = order.size();
  29.     std::vector<bool> positionDone(s+1, false);
  30.     for (unsigned i = 0; i < s; i++) {
  31.         int pos = order[i];
  32.         bool implyValue = imply(values[pos], values[pos + 1]);
  33.         if (verbose)
  34.             std::cout << pos << ' ' << values[pos] << "->" << values[pos+1] << " = " << implyValue << std::endl;
  35.         positionDone[pos] = true;
  36.         positionDone[pos+1] = true;
  37.  
  38.         unsigned iteratePos = pos;
  39.         bool shouldBreak = false;
  40.         while (!shouldBreak) {
  41.             if (positionDone[iteratePos]) {
  42.                 values[iteratePos] = implyValue;
  43.                 if (iteratePos == 0)
  44.                     shouldBreak = true;
  45.                 iteratePos--;
  46.             } else {
  47.                 shouldBreak = true;
  48.             }
  49.         }
  50.         shouldBreak = false;
  51.         iteratePos = pos + 1;
  52.         while (!shouldBreak) {
  53.             if (positionDone[iteratePos]) {
  54.                 values[iteratePos] = implyValue;
  55.                 if (iteratePos >= s)
  56.                     shouldBreak = true;
  57.                 iteratePos++;
  58.             } else {
  59.                 shouldBreak = true;
  60.             }
  61.         }
  62.         if (verbose)
  63.             printVector(values);
  64.     }
  65.     return values[0];
  66. }
  67.  
  68. int main(int argc, char *argv[]) {
  69.     if (argc > 1) {
  70.         SIZE = atoi(argv[1]);
  71.         std::cout << "SIZE = " << argv[1] << std::endl;
  72.     }
  73.     std::vector<bool> values;
  74.     for (unsigned i = 0; i < SIZE / 2; i++)
  75.         values.push_back(false);
  76.     for (unsigned i = 0; i < SIZE / 2; i++)
  77.         values.push_back(true);
  78.  
  79.     do {
  80.         std::vector<unsigned> order;
  81.         for (unsigned i = 0; i < SIZE - 1; i++) {
  82.             order.push_back(i);
  83.         }
  84.         if (values.back()) {
  85.             std::cout << "advance" << std::endl;
  86.         } else {
  87.         bool secondPlayerWin = false;
  88.         do {
  89.             if (!applyLogic(values, order)) {
  90.                 printVector(order);
  91.                 secondPlayerWin = true;
  92.                 break;
  93.             }
  94.         } while (std::next_permutation(order.begin(),order.end()));
  95.         printVector(values);
  96.         if (secondPlayerWin) {
  97.             std::cout << "second player wins this combination" << std::endl;
  98.         } else {
  99.             std::cout << "first player found the winning strategy!" << std::endl;
  100.             //do {applyLogic(values, order, true);} while (std::next_permutation(order.begin(),order.end()));
  101.             break;
  102.         }
  103.         }
  104.     } while (std::next_permutation(values.begin(),values.end()));
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement