#include "_.h"

int h = 103;
int w = 101;

void move_robot(int **pos) {
    int *pos_robot = *pos;

    pos_robot[0] += pos_robot[2];
    if (pos_robot[0] > w - 1)
        pos_robot[0] -= w;
    if (pos_robot[0] < 0)
        pos_robot[0] += w;
    pos_robot[1] += pos_robot[3];
    if (pos_robot[1] > h - 1)
        pos_robot[1] -= h;
    if (pos_robot[1] < 0)
        pos_robot[1] += h;
}

void display(int **pos_robot) {
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            int nbr = 0;
            for (int w = 0; pos_robot[w] != NULL; w++)
                if (pos_robot[w][0] == i && pos_robot[w][1] == j)
                    nbr++;

            if (nbr <= 0)
                printf(".");
            else
                printf("%d", nbr);
        }
        printf("\n");
    }
}

int main(int argc, char **argv) {
	
	if (argc != 2)
		return 1;

	char *input = argv[1];
	char **inputs = str_split(strdup(input), '\n');

    int **pos_robot = calloc(500 + 1,  sizeof(int*));
    
	for (int i = 0; inputs[i] != NULL; i++) {
        char **inputs_space = str_split(strdup(inputs[i]), ' ');

        pos_robot[i] = calloc(4 + 1,  sizeof(int));
        pos_robot[i][0] = atoi(inputs_space[0]);
        pos_robot[i][1] = atoi(inputs_space[1]);
        pos_robot[i][2] = atoi(inputs_space[2]);
        pos_robot[i][3] = atoi(inputs_space[3]);
	}

    for (int j = 0; j < 10404; j++) {
        for (int i = 0; pos_robot[i] != NULL; i++) {
            move_robot(&(pos_robot[i]));
        }
        printf("j: %d\n", j);
        display(pos_robot);
    }

    int count[4] = { [0 ... 3] = 0};
    for (int i = 0; pos_robot[i] != NULL; i++) {
        if (pos_robot[i][0] < w / 2 && pos_robot[i][1] < h / 2)
            count[0]++;
        if (pos_robot[i][0] > w / 2 && pos_robot[i][1] < h / 2)
            count[1]++;
        if (pos_robot[i][0] < w / 2 && pos_robot[i][1] > h / 2)
            count[2]++;
        if (pos_robot[i][0] > w / 2 && pos_robot[i][1] > h / 2)
            count[3]++;
    }

    int result = 1;
    for (int i = 0; i < 4; i++)
        result *= count[i];

    //display(pos_robot);

    printf("result: %d\n", result);

	return 0;
}
