#include "vision.h"

#include <algorithm>
#include <cassert>
#include <vector>

template<class T> using vec = std::vector<T>;
using ll = long long;

vec<int> getVisionPattern1d() {
  return { 1, 2, 2, 1, 1, 2 };
}

int getMove1d( vec<int> v ) {
  assert( (int)v.size() % 2 == 1 );
  int k = (int)v.size() / 2;
  assert( v[k] == k );

  if( k == 1 ){
    if( v[0] > 1 ) return 0;
    if( v[2] > 1 ) return 2;
  }

  vec<vec<int>> pat2 = {
    { 2, 1, 2, 2, 1 },
    { 1, 2, 2, 1, 1 },
    { 1, 1, 2, 1, 2 }
  };

  if( std::find( pat2.begin(), pat2.end(), v ) != pat2.end() ) return 4;

  std::reverse( v.begin(), v.end() );
  assert( std::find( pat2.begin(), pat2.end(), v ) != pat2.end() );
  return 0;
}

vec<vec<int>> getVisionPattern2d() {
  vec<int> d1 = getVisionPattern1d();
  vec<vec<int>> ret(6, vec<int>(6, 1));
  for( int i = 0; i < 6; i++ )
    for( int j = 0; j < 6; j += 3 )
      ret[(i + j) % 6][i] = d1[i];
  return ret;
}

std::pair<int, int> getMove2d( vec<vec<int>> v ) {
  assert( (int)v.size() % 2 == 1 );
  int k = (int)v.size() / 2;
  assert( v[k][k] == k );

  if( k == 1 ){
    for( int i = 0; i < 3; i++ )
      for( int j = 0; j < 3; j++ )
	if( v[i][j] > 1 ) return { i, j };
    assert( false );
  }

  assert( k == 2 );
  vec<int> diag(5);
  for( int i = 0; i < 5; i++ )
    diag[i] = v[i][i];
  int ret = getMove1d( diag );
  return { ret, ret };
}
