#include <bits/stdc++.h>
#include "vision.h"

using namespace std;

vector<int> t = {2, 1, 1, 3, 1, 2};

std::vector<int> getVisionPattern1d() {
  return t;
}

bool chk( vector<int> p ) {
  int n = t.size();
  vector<int> dt(2 * n);
  for ( int i = 0; i < 2 * n; ++i ) {
    dt[i] = t[i % n];
  }
  for ( int i = 0; i <= 2 * n - p.size(); ++i ) {
    bool ok = true;
    for ( int j = i; j < i + p.size(); ++j ) {
      ok &= (dt[j] == p[j - i]);
    }
    if ( ok ) return true;
  }
  return false;
}

int getMove1d(std::vector<int> v) {
  if ( chk(v) ) return v.size() - 1;
  return 0;
}

vector<vector<int>> t2 = {
  {1, 2, 3},
  {3, 2, 1},
  {2, 1, 3},
};

bool chk2( vector<vector<int>> p ) {
  int n = t2.size();
  int m = t2[0].size();
  vector<vector<int>> dt2(2 * n, vector<int>(2 * m));
  for ( int i = 0; i < 2 * n; ++i ) {
    for ( int j = 0; j < 2 * m; ++j ) {
      dt2[i][j] = t2[i % n][j % m];
    }
  }
  int q = p.size();
  for ( int i = 0; i <= 2 * n - q; ++i ) {
    for ( int j = 0; j <= 2 * m - q; ++j ) {
      bool ok = true;
      for ( int k = i; k < i + q; ++k ) {
        for ( int l = j; l < j + q; ++l ) {
          ok &= (dt2[k][l] == p[k - i][l - j]);
        }
      }
      if ( ok ) return true;
    }
  }
  return false;
}

std::vector<std::vector<int>> getVisionPattern2d() {
  return t2;
}

std::pair<int, int> getMove2d(std::vector<std::vector<int>> v) {
  if ( chk2(v) ) return {v.size() - 1, v.size() - 1};
  auto vv = v;
  for ( int i = 0; i < v.size(); ++i ) {
    reverse(v[i].begin(), v[i].end());
  }
  if ( chk2(v) ) return {v.size() - 1, 0};
  for ( int j = 0; j < v.size(); ++j ) {
    for ( int i = 0; i < v.size() / 2; ++i ) {
      swap(v[i][j], v[v.size() - i - 1][j]);
    }
  }
  if ( chk2(v) ) return {0, 0};

  return {0, v.size() - 1};
  /*for ( int j = 0; j < vv.size(); ++j ) {
    for ( int i = 0; i < vv.size() / 2; ++i ) {
      swap(vv[i][j], vv[vv.size() - i - 1][j]);
    }
  }
  if ( chk2() )*/
}
