day 4-2 in <1ms

This commit is contained in:
laura 2025-12-12 09:06:06 +01:00
parent c32f519997
commit 2e4c79664f

View file

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