#include #include #include #include #include #include #include #include #define ANSI_COLOR_RED "\x1b[31m" #define ANSI_COLOR_GREEN "\x1b[32m" #define ANSI_COLOR_RESET "\x1b[0m" char input[] = { #embed "input.txt" ,'\0' // null terminator ,'\0' }; int width = 0; int height = 0; uint64_t power(uint64_t base, uint64_t exp) { uint64_t i, result = 1; for (i = 0; i < exp; i++) result *= base; return result; } void print_field(uint8_t* surrounds){ for(int x = 0; x < height; x++){ for(int y = 0; y < width; y++){ if((surrounds[(x)*(width+2)+(y)]&0x80) == 0x80){ printf(ANSI_COLOR_GREEN"%d\t", (surrounds[(x)*(width+2)+(y)]-0x80)); }else{ //printf("%c", input[(x)*(width+1)+(y)]); printf(ANSI_COLOR_RED "%d\t", surrounds[(x)*(width+2)+(y)]); } } printf(ANSI_COLOR_RESET "\n"); } printf("==============================================\n"); } int main(int argc, char *argv[]){ clock_t start, end; start = clock(); while(input[width] != '\n'){ width++; } //printf("width: %d\n", width); while(input[height*(width+1)+1] != 0){ height++; } //printf("height: %d\n", height); uint8_t* surrounds = calloc((height+2)*(width+2), sizeof(uint8_t))+width+1; for(int x = 0; x < height; x++){ for(int y = 0; y< width; y++){ if(input[(x)*(width+1)+(y)] == '@'){ surrounds[(x-1)*(width+2)+(y-1)] += 1; surrounds[(x-1)*(width+2)+(y)] += 1; surrounds[(x-1)*(width+2)+(y+1)] += 1; surrounds[(x)*(width+2)+(y-1)] += 1; surrounds[(x)*(width+2)+(y)] += 0x80; surrounds[(x)*(width+2)+(y+1)] += 1; surrounds[(x+1)*(width+2)+(y-1)] += 1; surrounds[(x+1)*(width+2)+(y)] += 1; surrounds[(x+1)*(width+2)+(y+1)] += 1; } } } uint64_t count = 0; uint64_t count_2 = 0; bool cont = true; int* removes = calloc((height)*(width)*2, sizeof(int)); int* new_removes = calloc((height)*(width)*2, sizeof(int)); int new_count_2 = 0; count_2 = 0; for(int x = 0; x < height; x++){ for(int y = 0; y < width; y++){ if((surrounds[(x)*(width+2)+(y)]&0x7f) < 4 && (surrounds[(x)*(width+2)+(y)]&0x80) == 0x80){ removes[count_2*2] = x; removes[count_2*2+1] = y; count_2++; surrounds[(x)*(width+2)+(y)] -= 0x80; } } } count += count_2; while (count_2 != 0){ for(int i = 0; i < count_2; i++){ int x = removes[i*2]; int y = removes[i*2+1]; surrounds[(x-1)*(width+2)+(y-1)] -= 1; surrounds[(x-1)*(width+2)+(y)] -= 1; surrounds[(x-1)*(width+2)+(y+1)] -= 1; surrounds[(x)*(width+2)+(y-1)] -= 1; surrounds[(x)*(width+2)+(y+1)] -= 1; surrounds[(x+1)*(width+2)+(y-1)] -= 1; surrounds[(x+1)*(width+2)+(y)] -= 1; surrounds[(x+1)*(width+2)+(y+1)] -= 1; } new_count_2 = 0; for(int i = 0; i < count_2; i++){ int x = removes[i*2]; int y = removes[i*2+1]; for(int xd = -1; xd<=1; xd++){ for(int yd = -1; yd<=1; yd++){ if((surrounds[(x+xd)*(width+2)+(y+yd)]) < 132 && (surrounds[(x+xd)*(width+2)+(y+yd)]) >= 128){ new_removes[new_count_2*2] = x+xd; new_removes[new_count_2*2+1] = y+yd; surrounds[(x+xd)*(width+2)+(y+yd)] -= 0x80; new_count_2++; //} } } } } memcpy(removes, new_removes, new_count_2*2*sizeof(int)); count_2 = new_count_2; count += count_2; } free(removes); free(surrounds-width-1); end = clock(); printf("count: %lu\n", count); clock_t ticks_taken = end - start; double time_taken = ((double)ticks_taken)/CLOCKS_PER_SEC; printf("time take: %f ticks\n", time_taken); printf("CLOCKS_PER_SEC: %ld\n", CLOCKS_PER_SEC); exit(1); }