#include "_.h"

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

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

    int count = 0;
    int max_y = strslen(inputs);
    int max_x = strlen(inputs[0]);
    char display[max_y][max_x];
    for (int i = 0; i < max_y; i++)
        for (int j = 0; j < max_x; j++)
            display[i][j] = '.';
    int signals[4096][2] = { [0 ... 4095] = {-1, -1} };
	for (int y = 0; inputs[y] != NULL; y++) {
	    for (int x = 0; inputs[y][x] != '\0'; x++) {

            if (!isalpha(inputs[y][x]) && !isdigit(inputs[y][x]))
                continue;
            for (int i = 0; inputs[i] != NULL; i++) {
                for (int j = 0; inputs[i][j] != '\0'; j++) {

                if (!isalpha(inputs[i][j]) && !isdigit(inputs[i][j]))
                    continue;

                
                for (int w = 0; w < 1000; w++) {
                    if (i == y && j == x)
                        break;
                    if (inputs[i][j] != inputs[y][x])
                        break;

                    int signal_y = ((i - y) * w + i);
                    int signal_x = ((j - x) * w + j);

                    if (signal_y >= max_y ||
                        signal_y < 0 ||
                        signal_x >= max_x ||
                        signal_x < 0) {
                        break;
                    }

                    int continu = 0;
                    for (int v = 0; signals[v][0] != -1; v++) {
                       if (signals[v][0] == signal_y &&
                           signals[v][1] == signal_x) {
                            continu = 1;
                            break;
                       }
                    }
                    if (continu == 1)
                        continue;

                    display[signal_y][signal_x] = '#';
                    signals[count][0] = signal_y;
                    signals[count][1] = signal_x;
                    count+=1;
                }

                }
            }
        }
	}

    for (int i = 0; i < max_y; i++) {
        for (int j = 0; j < max_x; j++) {
            printf("%c", display[i][j]);
        }
        printf("\n");
    }
    printf("count: %d\n", count);
	return 0;
}
