diff --git a/4_2/main.c b/4_2/main.c index ba9f621..57229db 100644 --- a/4_2/main.c +++ b/4_2/main.c @@ -28,14 +28,16 @@ uint64_t power(uint64_t base, uint64_t exp) { 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)] < 4 &&*/ input[(x)*(width+1)+(y)] == '@'){ - printf("%d", surrounds[(x)*(width+2)+(y)]); + if((surrounds[(x)*(width+2)+(y)]&0x80) == 0x80){ + printf("%d", surrounds[(x)*(width+2)+(y)]-0x80); }else{ - printf("%c", input[(x)*(width+1)+(y)]); + //printf("%c", input[(x)*(width+1)+(y)]); + printf("."); } } printf("\n"); } + printf("==============================================\n"); } int main(int argc, char *argv[]){ @@ -62,7 +64,7 @@ int main(int argc, char *argv[]){ 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)] += 0; + 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; @@ -76,58 +78,63 @@ int main(int argc, char *argv[]){ } //printf("\n"); } + //print_field(surrounds); uint64_t count = 0; uint64_t count_2 = 0; bool cont = true; + int* removes = calloc((height)*(width)*2, sizeof(int)); + while (cont){ count_2 = 0; for(int x = 0; x < height; x++){ for(int y = 0; y < width; y++){ - if(surrounds[(x)*(width+2)+(y)] < 4 && input[(x)*(width+1)+(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++; - input[(x)*(width+1)+(y)] = 'x'; + //printf("removing %d, %d\n", x, y); + //input[(x)*(width+1)+(y)] = 'x'; //printf("%d", surrounds[(x)*width+(y)]); - }else{ - //printf("%c", input[(x)*width+(y)]); - } + }//if((surrounds[(x)*(width+2)+(y)]&0x7f) > 8 && (surrounds[(x)*(width+2)+(y)]&0x80) == 0x80){ + //printf("underflow detechted, aborting\n"); + //cont = false; + //} } //printf("\n"); } //print_field(surrounds); count += count_2; + + //printf("count = %lu\n========\n", count); if(count_2 == 0){ cont = false; } //printf("cont: %s\n", cont ? "true" : "false"); //printf("count_2 = %lu\n", count_2); //printf("count = %lu\n", count); - for(int x = 0; x < height; x++){ - for(int y = 0; y < width; y++){ - if(input[(x)*(width+1)+(y)] == 'x'){ - 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)] -= 0; - 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; - input[(x)*(width+1)+(y)] = '.'; - //printf("%d", surrounds[(x)*width+(y)]); - }else{ - //printf("%c", input[(x)*width+(y)]); - } - } - //printf("\n"); + + + 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)] -= 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; } //print_field(surrounds); //sleep(1); } - - free(surrounds-width-1); + //free(removes); + //free(surrounds-width-1); end = clock(); printf("count: %lu\n", count);