#include "lib.h"
#include <random>
#include <algorithm>
#include <unistd.h>
#include <cassert>
#include <cstring>
#include <fcntl.h>

using namespace lib;
using namespace internal;

// HACK for using lib
const int VERSION=1;
const bool ALLOW_MISTAKES=0;

int main(int argc, char ** argv)
{
	const char * mode = "";
	if(argc > 1) mode=argv[1];

	int n;
	vector<int> in;
	vector<bool> out;
	char * verdict;
	bool mistakes;
	load_data(n, in, out, verdict, mistakes);

	auto other = gen_arr_other_car_of_type(n, in);

	printf("n: %d       %s\n", n, mistakes?"\e[31mMISTAKES ALLOWED\e[0m":"");
	printf("score: %d   mistakes: %d \n", calc_score(n, out), calc_mistakes(n, in, out));
	fflush(stdout);

	FILE * jq_pipe = popen("jq", "w");
	fprintf(jq_pipe, "%s", verdict);
	pclose(jq_pipe);

	auto print_color = [&](int i){
		printf(out[i] ? "\e[91m" : "\e[92m");
		if(out[i] == out[other[i]])
			printf("\e[44m");
	};
	auto print_int = [](int val, int bound){
		int div = 1;
		while(10*div <= bound) div*=10;
		for(;div;div/=10) putchar(val/div%10 + '0');
	};

	if(!strcmp(mode, "n"))
	{
		fo(i, 2*n)
		{
			if(i) putchar(' ');
			print_color(i);
			print_int(in[i], n);
			printf("\e[0m");
		}
		printf("\n");
	}
	else
	if(!strcmp(mode, "nd"))
	{
		fo(i, 2*n)
		{
			if(i) putchar(' ');
			print_color(i);
			if(i > other[i]) putchar('<');
			print_int(in[i], n);
			if(i < other[i]) putchar('>');
			printf("\e[0m");
		}
		printf("\n");
	}
	else
	if(!strcmp(mode, "d"))
	{
		fo(i, 2*n)
		{
			print_color(i);
			putchar(i<other[i]?'>':'<');
			printf("\e[0m");
		}
		printf("\n");
	}
	else
	if(!strcmp(mode, "h"))
	{
		fo(i, 2*n)
		{
			print_color(i);
			printf("#\e[0m");
		}
		printf("\n");
	}


}