#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>

#define ALGS 8
#define MAX_SIZE 32768

#ifndef UINT32_MAX
#define UINT32_MAX (4294967295U)
#endif


int main(int intc, char **argv){
	FILE *in;
	uint32_t a, b; //, c, d, e;
	uint32_t num1, num2 = 0;
	uint32_t cur_cycles = UINT32_MAX;
	uint32_t switches = 0;
	uint32_t cur_fastest = 0;
	uint32_t prev_fastest = 0;
	uint32_t wins[8] = {0,0,0,0,0,0,0,0};
	struct Streaks {
		uint32_t current;
		uint32_t cur_s;
		uint32_t cur_e;
		uint32_t longest;
		uint32_t start;
		uint32_t end;
	}streaks[8] = {{0,0,0,0,0,0},{0,0,0,0,0,0},{0,0,0,0,0,0},{0,0,0,0,0,0},{0,0,0,0,0,0},{0,0,0,0,0,0},{0,0,0,0,0,0},{0,0,0,0,0,0}};

	struct _pos_ranges {
		size_t range_spot;
		size_t selected_alg;
		size_t start_pos;
		size_t end_pos;
		size_t wins;
	}range_;

	size_t range_wins = 0;
	size_t range_wins_max = 0;
	size_t range_winner[4][1][1];
	size_t ranges[4][ALGS][MAX_SIZE];
	size_t data[ALGS][MAX_SIZE];
        uint32_t read_buffer_size = 64;
        char *read_buffer = (char *)malloc(read_buffer_size + 1);
        (void)memset( (void *)read_buffer, '\0', read_buffer_size + 1);

        for(num1=0; num1<ALGS; num1++){
#ifdef IS32BIT
                if(num1 == 5)
                        continue;
#endif
                (void)sprintf(read_buffer, "results/r%d.txt", num1+1);
                in = fopen(read_buffer, "r");
                num2 = 0;
                (void)memset( (void *)read_buffer, '\0', read_buffer_size + 1);

                while( fgets(read_buffer, read_buffer_size, in) != NULL){
                        if(num2 == MAX_SIZE)
                                break;
                        data[num1][num2] = atoi(read_buffer);
                        num2++;
                }
                fclose(in);
        }
	free(read_buffer);


	prev_fastest = ALGS+1;
	for(a=0; a<MAX_SIZE; a++){

		//find fastest of 8 algs at current size
		cur_cycles = UINT32_MAX;
		for(b=0; b<ALGS; b++){
			if(data[b][a]<cur_cycles){
				cur_fastest = b;
				cur_cycles = data[b][a];
			}
		}
		if(cur_fastest == prev_fastest){
			streaks[cur_fastest].current++;
		}else{
			streaks[cur_fastest].current = 1;
			streaks[cur_fastest].cur_s = a;
		}

		//if(streaks[cur_fastest].current == 1)
		//	streaks[cur_fastest].cur_s = a;

		if(streaks[cur_fastest].current > streaks[cur_fastest].longest){
			streaks[cur_fastest].longest = streaks[cur_fastest].current;
			streaks[cur_fastest].end = a+1;
			streaks[cur_fastest].start = streaks[cur_fastest].cur_s;
		}

		if(cur_fastest != prev_fastest){
			switches++;
			prev_fastest = cur_fastest;
		}

		wins[cur_fastest]++;

	}

	printf("switches:\t%u\n", switches);
	printf("\twins\tlongest\t(start\t-\tend)\n");
	for(a=0; a<8; a++){
		printf("%u:\t%u\t%u\t(%u\t-\t%u)\n", a, wins[a], streaks[a].longest, streaks[a].start, streaks[a].end);
	}

	for(a=0; a<4; a++){
		for(b=0; b<ALGS; b++){
			//determine c start
			 
			d = 
			for(c=0; c<MAX_SIZE; c++){
				//ranges[a][b][c]
				//calculate range wins of this potential solution
				range_wins = 0;
				for(num1=0; num1<MAX_SIZE; num1++){
					data[b][a]
				}
				//if better than previous
				if(range_wins > range_wins_max){
					//range_winner[4][1][1]
				}
			}
		}
	}

	return 0;
}
