#include "_.h" int count = 0; long check(int ***already, char **inputs, int y, int x) { (*already)[y][x] = inputs[y][x]; long perimeter = 0; if (inputs[y + 1] != NULL && inputs[y][x] == inputs[y + 1][x]) { if ((*already)[y + 1][x] == 0) perimeter += check(already, inputs, y + 1, x); } else perimeter += 1; if (y > 0 && inputs[y][x] == inputs[y - 1][x]) { if ((*already)[y - 1][x] == 0) perimeter += check(already, inputs, y - 1, x); } else perimeter += 1; if (x < strlen(inputs[y]) - 1 && inputs[y][x] == inputs[y][x + 1]) { if ((*already)[y][x + 1] == 0) perimeter += check(already, inputs, y, x + 1); } else perimeter += 1; if (x > 0 && inputs[y][x] == inputs[y][x - 1]) { if ((*already)[y][x - 1] == 0) perimeter += check(already, inputs, y, x - 1); } else perimeter += 1; count += 1; return perimeter; } int main(int argc, char **argv) { if (argc != 2) return 1; char *input = argv[1]; char **inputs = str_split(strdup(input), '\n'); int len = strlen(inputs[0]); int lens = strslen(inputs); int **already = calloc(lens + 1, sizeof(int*)); for (int i = 0; i == 0 || inputs[i - 1] != NULL; i++) already[i] = calloc(len + 1, sizeof(int)); long result = 0; for (int i = 0; inputs[i] != NULL; i++) { for (int j = 0; inputs[i][j] != '\0'; j++) { if (already[i][j] == 0) { int perimeter = (check(&already, inputs, i, j)); result += (count * perimeter); //printf("perimeter: %d\n", perimeter); //printf("count: %d\n", count); //printf("result: %ld\n", result); count = 0; } } } printf("result: %ld\n", result); return 0; }