#pragma GCC optimize("-fno-strict-aliasing")
#include<csignal>
#include<iostream>
using namespace std;

#ifdef solution
#define Solution solutions[solution]
#include"../solutions.hpp"
#else
#include"../graph.hpp"
#endif

#include"Interface.hpp"
#include"Indexed.hpp"

// There is no iteration cap. Time, or one of these signals, stops the run.
static volatile sig_atomic_t stop_req=0;

static void on_stop(int){
  stop_req=1;
}

static void paint(Indexed*const indexed){
  cout<<'{';
  for(unsigned i=0;i<nVertices;++i)
    cout<<(indexed->vertices[i].selected-i-indexed->movements[0])/nVertices<<',';
  cout<<"},"<<endl;
}

int main(){
  struct sigaction sa={};
  sa.sa_handler=on_stop;
  sigemptyset(&sa.sa_mask);
  sigaction(SIGTERM,&sa,nullptr);
  sigaction(SIGHUP,&sa,nullptr);
  sigaction(SIGINT,&sa,nullptr);

  Indexed*const indexed=new Indexed;
  indexed->start();
  while(!stop_req){
    while(indexed->zerocolor.score&&!stop_req)
      indexed->solve();
    if(stop_req)break;
    paint(indexed);
    indexed->shrink();
  }
  // Coloring at the stop. It can seed a later run.
  paint(indexed);
  delete indexed;
  return 0;
}
