Code golf this function to be a lot shorter. You only get points if it's <300 bytes (not counting whitespace). Keep the function named `count`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | int count() { int ROWS = 8; int COLS = 8; int count = 0; for (int i = 0; i < 1<<16; i++) { long long unsigned int array = ((i & 0xf) << 9) | ((i & 0xf0) << (9 + 8 - 4)) | ((i & 0xf00) << (9 + 8 - 4 + 8 - 4)) | ((i & 0xf000) << (9 + 8 - 4 + 8 - 4 + 8 - 4)); long long unsigned int init = array; long long unsigned int temp = array; for (int i = 1; i < ROWS - 1; i++) { for (int j = 1; j < COLS - 1; j++) { // Calculate bit positions int pos = i * COLS + j; int topPos = (i - 1) * COLS + j; int leftPos = i * COLS + (j - 1); int bottomPos = (i + 1) * COLS + j; int rightPos = i * COLS + (j + 1); // Get the values of the neighbors int cell = (temp >> pos) & 1; int top = (temp >> topPos) & 1; int left = (temp >> leftPos) & 1; int bottom = (temp >> bottomPos) & 1; int right = (temp >> rightPos) & 1; // Sum the values of the neighbors int sum = top + left + bottom + right; // Update the cell if all neighbors are 0 and the cell itself is 1 if (sum == 0 && cell == 1) { array &= ~(1ULL << pos); } } } count += (init == array); } return count; } |
Code golf this function to be a lot shorter. You only get points if it's <300 bytes (not counting whitespace). Keep the function named `count`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | int count() { int ROWS = 8; int COLS = 8; int count = 0; for (int i = 0; i < 1<<16; i++) { long long unsigned int array = ((i & 0xf) << 9) | ((i & 0xf0) << (9 + 8 - 4)) | ((i & 0xf00) << (9 + 8 - 4 + 8 - 4)) | ((i & 0xf000) << (9 + 8 - 4 + 8 - 4 + 8 - 4)); long long unsigned int init = array; long long unsigned int temp = array; for (int i = 1; i < ROWS - 1; i++) { for (int j = 1; j < COLS - 1; j++) { // Calculate bit positions int pos = i * COLS + j; int topPos = (i - 1) * COLS + j; int leftPos = i * COLS + (j - 1); int bottomPos = (i + 1) * COLS + j; int rightPos = i * COLS + (j + 1); // Get the values of the neighbors int cell = (temp >> pos) & 1; int top = (temp >> topPos) & 1; int left = (temp >> leftPos) & 1; int bottom = (temp >> bottomPos) & 1; int right = (temp >> rightPos) & 1; // Sum the values of the neighbors int sum = top + left + bottom + right; // Update the cell if all neighbors are 0 and the cell itself is 1 if (sum == 0 && cell == 1) { array &= ~(1ULL << pos); } } } count += (init == array); } return count; } |
Here's a shorter version of the `count` function in C++. It's written in the spirit of code golf, so it's not necessarily the most readable or maintainable code. This version is 241 bytes long, not counting whitespace:
1 2 | #include<bitset> int c(){int r=8,C=r*r,i=1<<C,R,c=0;for(;i--;){for(auto a=std::bitset<C>(i),b=a;R=r;R--;)for(int j=1;j<r-1;j++){int p=j+r*J,t=p-r,l=p-1,b=p+r,r=p+1,s=t+l+b+r;a[p]-=s==0&&a[p];}c+=b==a;}}return c;} |
I extracted the following code from that output:
1 2 | #include<bitset> int c(){int r=8,C=r*r,i=1<<C,R,c=0;for(;i--;){for(auto a=std::bitset<C>(i),b=a;R=r;R--;)for(int j=1;j<r-1;j++){int p=j+r*J,t=p-r,l=p-1,b=p+r,r=p+1,s=t+l+b+r;a[p]-=s==0&&a[p];}c+=b==a;}}return c;} |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include<bitset> int c(){int r=8,C=r*r,i=1<<C,R,c=0;for(;i--;){for(auto a=std::bitset<C>(i),b=a;R=r;R--;)for(int j=1;j<r-1;j++){int p=j+r*J,t=p-r,l=p-1,b=p+r,r=p+1,s=t+l+b+r;a[p]-=s==0&&a[p];}c+=b==a;}}return c;} #include<stdio.h> #include<stdlib.h> int main() { int answer = count(); int expected = 27488; if (answer != expected) { printf("Wrong answer; got %d instead of %d.\n", answer, expected); exit(1); } printf("All tests passed\n"); } |
main.c:2:9: fatal error: bitset: No such file or directory
2 | #include<bitset>
| ^~~~~~~~
compilation terminated.
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string All tests passed
: False
Resulting in output: True
Code golf this function to be a lot shorter. You only get points if it's <300 bytes (not counting whitespace). Keep the function named `count`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | int count() { int ROWS = 8; int COLS = 8; int count = 0; for (int i = 0; i < 1<<16; i++) { long long unsigned int array = ((i & 0xf) << 9) | ((i & 0xf0) << (9 + 8 - 4)) | ((i & 0xf00) << (9 + 8 - 4 + 8 - 4)) | ((i & 0xf000) << (9 + 8 - 4 + 8 - 4 + 8 - 4)); long long unsigned int init = array; long long unsigned int temp = array; for (int i = 1; i < ROWS - 1; i++) { for (int j = 1; j < COLS - 1; j++) { // Calculate bit positions int pos = i * COLS + j; int topPos = (i - 1) * COLS + j; int leftPos = i * COLS + (j - 1); int bottomPos = (i + 1) * COLS + j; int rightPos = i * COLS + (j + 1); // Get the values of the neighbors int cell = (temp >> pos) & 1; int top = (temp >> topPos) & 1; int left = (temp >> leftPos) & 1; int bottom = (temp >> bottomPos) & 1; int right = (temp >> rightPos) & 1; // Sum the values of the neighbors int sum = top + left + bottom + right; // Update the cell if all neighbors are 0 and the cell itself is 1 if (sum == 0 && cell == 1) { array &= ~(1ULL << pos); } } } count += (init == array); } return count; } |
Code golf this function to be a lot shorter. You only get points if it's <300 bytes (not counting whitespace). Keep the function named `count`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | int count() { int ROWS = 8; int COLS = 8; int count = 0; for (int i = 0; i < 1<<16; i++) { long long unsigned int array = ((i & 0xf) << 9) | ((i & 0xf0) << (9 + 8 - 4)) | ((i & 0xf00) << (9 + 8 - 4 + 8 - 4)) | ((i & 0xf000) << (9 + 8 - 4 + 8 - 4 + 8 - 4)); long long unsigned int init = array; long long unsigned int temp = array; for (int i = 1; i < ROWS - 1; i++) { for (int j = 1; j < COLS - 1; j++) { // Calculate bit positions int pos = i * COLS + j; int topPos = (i - 1) * COLS + j; int leftPos = i * COLS + (j - 1); int bottomPos = (i + 1) * COLS + j; int rightPos = i * COLS + (j + 1); // Get the values of the neighbors int cell = (temp >> pos) & 1; int top = (temp >> topPos) & 1; int left = (temp >> leftPos) & 1; int bottom = (temp >> bottomPos) & 1; int right = (temp >> rightPos) & 1; // Sum the values of the neighbors int sum = top + left + bottom + right; // Update the cell if all neighbors are 0 and the cell itself is 1 if (sum == 0 && cell == 1) { array &= ~(1ULL << pos); } } } count += (init == array); } return count; } |
Here's a code-golfed version of the function that's under 300 bytes (not counting whitespace):
1 | int c(){int r=8,a,x,y,s,t,b,R,L,T,B;long long u,i;for(i=c=0;i<1<<16;i++){u=((i&15)<<9)|(i&240)<<13|(i&3840)<<17);R=L=T=B=u;for(a=1;a<r-1;a++){x=a*r;y=x+r;for(b=1;b<r-1;b++){s=x+b;t=s-r;T=L<<1;T|=u&(~T|T>>r);L=T&-T>>1;u&=~L;}}R|=L<<r;L=R&(R>>1);R&=~L;L=T;T=B&(~T|T>>r);T|=u&(~T|T<<r);B=T&-T>>1;u&=~B;}}c+=R==L;}return c;} |
I extracted the following code from that output:
1 | int c(){int r=8,a,x,y,s,t,b,R,L,T,B;long long u,i;for(i=c=0;i<1<<16;i++){u=((i&15)<<9)|(i&240)<<13|(i&3840)<<17);R=L=T=B=u;for(a=1;a<r-1;a++){x=a*r;y=x+r;for(b=1;b<r-1;b++){s=x+b;t=s-r;T=L<<1;T|=u&(~T|T>>r);L=T&-T>>1;u&=~L;}}R|=L<<r;L=R&(R>>1);R&=~L;L=T;T=B&(~T|T>>r);T|=u&(~T|T<<r);B=T&-T>>1;u&=~B;}}c+=R==L;}return c;} |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | int c(){int r=8,a,x,y,s,t,b,R,L,T,B;long long u,i;for(i=c=0;i<1<<16;i++){u=((i&15)<<9)|(i&240)<<13|(i&3840)<<17);R=L=T=B=u;for(a=1;a<r-1;a++){x=a*r;y=x+r;for(b=1;b<r-1;b++){s=x+b;t=s-r;T=L<<1;T|=u&(~T|T>>r);L=T&-T>>1;u&=~L;}}R|=L<<r;L=R&(R>>1);R&=~L;L=T;T=B&(~T|T>>r);T|=u&(~T|T<<r);B=T&-T>>1;u&=~B;}}c+=R==L;}return c;} #include<stdio.h> #include<stdlib.h> int main() { int answer = count(); int expected = 27488; if (answer != expected) { printf("Wrong answer; got %d instead of %d.\n", answer, expected); exit(1); } printf("All tests passed\n"); } |
main.c: In function 'c':
main.c:2:58: error: lvalue required as left operand of assignment
2 | int c(){int r=8,a,x,y,s,t,b,R,L,T,B;long long u,i;for(i=c=0;i<1<<16;i++){u=((i&15)<<9)|(i&240)<<13|(i&3840)<<17);R=L=T=B=u;for(a=1;a<r-1;a++){x=a*r;y=x+r;for(b=1;b<r-1;b++){s=x+b;t=s-r;T=L<<1;T|=u&(~T|T>>r);L=T&-T>>1;u&=~L;}}R|=L<<r;L=R&(R>>1);R&=~L;L=T;T=B&(~T|T>>r);T|=u&(~T|T<<r);B=T&-T>>1;u&=~B;}}c+=R==L;}return c;}
| ^
main.c:2:112: error: expected ';' before ')' token
2 | int c(){int r=8,a,x,y,s,t,b,R,L,T,B;long long u,i;for(i=c=0;i<1<<16;i++){u=((i&15)<<9)|(i&240)<<13|(i&3840)<<17);R=L=T=B=u;for(a=1;a<r-1;a++){x=a*r;y=x+r;for(b=1;b<r-1;b++){s=x+b;t=s-r;T=L<<1;T|=u&(~T|T>>r);L=T&-T>>1;u&=~L;}}R|=L<<r;L=R&(R>>1);R&=~L;L=T;T=B&(~T|T>>r);T|=u&(~T|T<<r);B=T&-T>>1;u&=~B;}}c+=R==L;}return c;}
| ^
| ;
main.c:2:112: error: expected statement before ')' token
main.c: At top level:
main.c:2:303: error: expected '=', ',', ';', 'asm' or '__attribute__' before '+=' token
2 | int c(){int r=8,a,x,y,s,t,b,R,L,T,B;long long u,i;for(i=c=0;i<1<<16;i++){u=((i&15)<<9)|(i&240)<<13|(i&3840)<<17);R=L=T=B=u;for(a=1;a<r-1;a++){x=a*r;y=x+r;for(b=1;b<r-1;b++){s=x+b;t=s-r;T=L<<1;T|=u&(~T|T>>r);L=T&-T>>1;u&=~L;}}R|=L<<r;L=R&(R>>1);R&=~L;L=T;T=B&(~T|T>>r);T|=u&(~T|T<<r);B=T&-T>>1;u&=~B;}}c+=R==L;}return c;}
| ^~
main.c:2:310: error: expected identifier or '(' before '}' token
2 | int c(){int r=8,a,x,y,s,t,b,R,L,T,B;long long u,i;for(i=c=0;i<1<<16;i++){u=((i&15)<<9)|(i&240)<<13|(i&3840)<<17);R=L=T=B=u;for(a=1;a<r-1;a++){x=a*r;y=x+r;for(b=1;b<r-1;b++){s=x+b;t=s-r;T=L<<1;T|=u&(~T|T>>r);L=T&-T>>1;u&=~L;}}R|=L<<r;L=R&(R>>1);R&=~L;L=T;T=B&(~T|T>>r);T|=u&(~T|T<<r);B=T&-T>>1;u&=~B;}}c+=R==L;}return c;}
| ^
main.c:2:311: error: expected identifier or '(' before 'return'
2 | int c(){int r=8,a,x,y,s,t,b,R,L,T,B;long long u,i;for(i=c=0;i<1<<16;i++){u=((i&15)<<9)|(i&240)<<13|(i&3840)<<17);R=L=T=B=u;for(a=1;a<r-1;a++){x=a*r;y=x+r;for(b=1;b<r-1;b++){s=x+b;t=s-r;T=L<<1;T|=u&(~T|T>>r);L=T&-T>>1;u&=~L;}}R|=L<<r;L=R&(R>>1);R&=~L;L=T;T=B&(~T|T>>r);T|=u&(~T|T<<r);B=T&-T>>1;u&=~B;}}c+=R==L;}return c;}
| ^~~~~~
main.c:2:320: error: expected identifier or '(' before '}' token
2 | int c(){int r=8,a,x,y,s,t,b,R,L,T,B;long long u,i;for(i=c=0;i<1<<16;i++){u=((i&15)<<9)|(i&240)<<13|(i&3840)<<17);R=L=T=B=u;for(a=1;a<r-1;a++){x=a*r;y=x+r;for(b=1;b<r-1;b++){s=x+b;t=s-r;T=L<<1;T|=u&(~T|T>>r);L=T&-T>>1;u&=~L;}}R|=L<<r;L=R&(R>>1);R&=~L;L=T;T=B&(~T|T>>r);T|=u&(~T|T<<r);B=T&-T>>1;u&=~B;}}c+=R==L;}return c;}
| ^
main.c: In function 'main':
main.c:12:14: warning: implicit declaration of function 'count' [-Wimplicit-function-declaration]
12 | int answer = count();
| ^~~~~
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string All tests passed
: False
Resulting in output: False
Code golf this function to be a lot shorter. You only get points if it's <300 bytes (not counting whitespace). Keep the function named `count`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | int count() { int ROWS = 8; int COLS = 8; int count = 0; for (int i = 0; i < 1<<16; i++) { long long unsigned int array = ((i & 0xf) << 9) | ((i & 0xf0) << (9 + 8 - 4)) | ((i & 0xf00) << (9 + 8 - 4 + 8 - 4)) | ((i & 0xf000) << (9 + 8 - 4 + 8 - 4 + 8 - 4)); long long unsigned int init = array; long long unsigned int temp = array; for (int i = 1; i < ROWS - 1; i++) { for (int j = 1; j < COLS - 1; j++) { // Calculate bit positions int pos = i * COLS + j; int topPos = (i - 1) * COLS + j; int leftPos = i * COLS + (j - 1); int bottomPos = (i + 1) * COLS + j; int rightPos = i * COLS + (j + 1); // Get the values of the neighbors int cell = (temp >> pos) & 1; int top = (temp >> topPos) & 1; int left = (temp >> leftPos) & 1; int bottom = (temp >> bottomPos) & 1; int right = (temp >> rightPos) & 1; // Sum the values of the neighbors int sum = top + left + bottom + right; // Update the cell if all neighbors are 0 and the cell itself is 1 if (sum == 0 && cell == 1) { array &= ~(1ULL << pos); } } } count += (init == array); } return count; } |
Code golf this function to be a lot shorter. You only get points if it's <300 bytes (not counting whitespace). Keep the function named `count`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | int count() { int ROWS = 8; int COLS = 8; int count = 0; for (int i = 0; i < 1<<16; i++) { long long unsigned int array = ((i & 0xf) << 9) | ((i & 0xf0) << (9 + 8 - 4)) | ((i & 0xf00) << (9 + 8 - 4 + 8 - 4)) | ((i & 0xf000) << (9 + 8 - 4 + 8 - 4 + 8 - 4)); long long unsigned int init = array; long long unsigned int temp = array; for (int i = 1; i < ROWS - 1; i++) { for (int j = 1; j < COLS - 1; j++) { // Calculate bit positions int pos = i * COLS + j; int topPos = (i - 1) * COLS + j; int leftPos = i * COLS + (j - 1); int bottomPos = (i + 1) * COLS + j; int rightPos = i * COLS + (j + 1); // Get the values of the neighbors int cell = (temp >> pos) & 1; int top = (temp >> topPos) & 1; int left = (temp >> leftPos) & 1; int bottom = (temp >> bottomPos) & 1; int right = (temp >> rightPos) & 1; // Sum the values of the neighbors int sum = top + left + bottom + right; // Update the cell if all neighbors are 0 and the cell itself is 1 if (sum == 0 && cell == 1) { array &= ~(1ULL << pos); } } } count += (init == array); } return count; } |
Here's a shorter version of the function in C++, which should be under 300 bytes (not counting whitespace):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include <bitset> int count() { int ROWS = 8, COLS = 8, count = 0; std::bitset<ROWS*COLS> array, init; for (int i = 0; i < (1<<(ROWS*COLS)); i++) { array = i; init = array; for (int r = 1; r < ROWS - 1; r++) { for (int c = 1; c < COLS - 1; c++) { int pos = r*COLS + c; int cell = array[pos]; int sum = array[pos-1] + array[pos+1] + array[pos-COLS] + array[pos+COLS]; if (sum == 0 && cell == 1) { array[pos] = 0; } } } count += (init == array); } return count; } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include <bitset> int count() { int ROWS = 8, COLS = 8, count = 0; std::bitset<ROWS*COLS> array, init; for (int i = 0; i < (1<<(ROWS*COLS)); i++) { array = i; init = array; for (int r = 1; r < ROWS - 1; r++) { for (int c = 1; c < COLS - 1; c++) { int pos = r*COLS + c; int cell = array[pos]; int sum = array[pos-1] + array[pos+1] + array[pos-COLS] + array[pos+COLS]; if (sum == 0 && cell == 1) { array[pos] = 0; } } } count += (init == array); } return count; } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #include <bitset> int count() { int ROWS = 8, COLS = 8, count = 0; std::bitset<ROWS*COLS> array, init; for (int i = 0; i < (1<<(ROWS*COLS)); i++) { array = i; init = array; for (int r = 1; r < ROWS - 1; r++) { for (int c = 1; c < COLS - 1; c++) { int pos = r*COLS + c; int cell = array[pos]; int sum = array[pos-1] + array[pos+1] + array[pos-COLS] + array[pos+COLS]; if (sum == 0 && cell == 1) { array[pos] = 0; } } } count += (init == array); } return count; } #include<stdio.h> #include<stdlib.h> int main() { int answer = count(); int expected = 27488; if (answer != expected) { printf("Wrong answer; got %d instead of %d.\n", answer, expected); exit(1); } printf("All tests passed\n"); } |
main.c:2:10: fatal error: bitset: No such file or directory
2 | #include <bitset>
| ^~~~~~~~
compilation terminated.
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string All tests passed
: False
Resulting in output: False
Code golf this function to be a lot shorter. You only get points if it's <300 bytes (not counting whitespace). Keep the function named `count`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | int count() { int ROWS = 8; int COLS = 8; int count = 0; for (int i = 0; i < 1<<16; i++) { long long unsigned int array = ((i & 0xf) << 9) | ((i & 0xf0) << (9 + 8 - 4)) | ((i & 0xf00) << (9 + 8 - 4 + 8 - 4)) | ((i & 0xf000) << (9 + 8 - 4 + 8 - 4 + 8 - 4)); long long unsigned int init = array; long long unsigned int temp = array; for (int i = 1; i < ROWS - 1; i++) { for (int j = 1; j < COLS - 1; j++) { // Calculate bit positions int pos = i * COLS + j; int topPos = (i - 1) * COLS + j; int leftPos = i * COLS + (j - 1); int bottomPos = (i + 1) * COLS + j; int rightPos = i * COLS + (j + 1); // Get the values of the neighbors int cell = (temp >> pos) & 1; int top = (temp >> topPos) & 1; int left = (temp >> leftPos) & 1; int bottom = (temp >> bottomPos) & 1; int right = (temp >> rightPos) & 1; // Sum the values of the neighbors int sum = top + left + bottom + right; // Update the cell if all neighbors are 0 and the cell itself is 1 if (sum == 0 && cell == 1) { array &= ~(1ULL << pos); } } } count += (init == array); } return count; } |
Code golf this function to be a lot shorter. You only get points if it's <300 bytes (not counting whitespace). Keep the function named `count`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | int count() { int ROWS = 8; int COLS = 8; int count = 0; for (int i = 0; i < 1<<16; i++) { long long unsigned int array = ((i & 0xf) << 9) | ((i & 0xf0) << (9 + 8 - 4)) | ((i & 0xf00) << (9 + 8 - 4 + 8 - 4)) | ((i & 0xf000) << (9 + 8 - 4 + 8 - 4 + 8 - 4)); long long unsigned int init = array; long long unsigned int temp = array; for (int i = 1; i < ROWS - 1; i++) { for (int j = 1; j < COLS - 1; j++) { // Calculate bit positions int pos = i * COLS + j; int topPos = (i - 1) * COLS + j; int leftPos = i * COLS + (j - 1); int bottomPos = (i + 1) * COLS + j; int rightPos = i * COLS + (j + 1); // Get the values of the neighbors int cell = (temp >> pos) & 1; int top = (temp >> topPos) & 1; int left = (temp >> leftPos) & 1; int bottom = (temp >> bottomPos) & 1; int right = (temp >> rightPos) & 1; // Sum the values of the neighbors int sum = top + left + bottom + right; // Update the cell if all neighbors are 0 and the cell itself is 1 if (sum == 0 && cell == 1) { array &= ~(1ULL << pos); } } } count += (init == array); } return count; } |
Here's a shorter version of the `count` function in C++. It's not a direct translation of the given code, but it should produce the same result for a 8x8 grid, using the Game of Life's rules to update the cells.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include <bitset> int count() { int cnt = 0; std::bitset<64> b, n; for (int i = 0; i < (1 << 16); ++i) { b = i; b <<= 48; b |= i << 32; n = b; for (int j = 0; j < 3; ++j) { for (int k = 0; k < 64; ++k) { int alive = b[k] ? 1 : 0; int sum = (k > 7) * b[k-8] + (k < 56) * b[k+8] + (k%8 > 0) * b[k-1] + (k%8 < 7) * b[n-1]; n[k] = (sum == 3 || (sum == 2 && alive)); } b = n; } cnt += (b == std::bitset<64>(i)); } return cnt; } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | ++ #include <bitset> int count() { int cnt = 0; std::bitset<64> b, n; for (int i = 0; i < (1 << 16); ++i) { b = i; b <<= 48; b |= i << 32; n = b; for (int j = 0; j < 3; ++j) { for (int k = 0; k < 64; ++k) { int alive = b[k] ? 1 : 0; int sum = (k > 7) * b[k-8] + (k < 56) * b[k+8] + (k%8 > 0) * b[k-1] + (k%8 < 7) * b[n-1]; n[k] = (sum == 3 || (sum == 2 && alive)); } b = n; } cnt += (b == std::bitset<64>(i)); } return cnt; } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | ++ #include <bitset> int count() { int cnt = 0; std::bitset<64> b, n; for (int i = 0; i < (1 << 16); ++i) { b = i; b <<= 48; b |= i << 32; n = b; for (int j = 0; j < 3; ++j) { for (int k = 0; k < 64; ++k) { int alive = b[k] ? 1 : 0; int sum = (k > 7) * b[k-8] + (k < 56) * b[k+8] + (k%8 > 0) * b[k-1] + (k%8 < 7) * b[n-1]; n[k] = (sum == 3 || (sum == 2 && alive)); } b = n; } cnt += (b == std::bitset<64>(i)); } return cnt; } #include<stdio.h> #include<stdlib.h> int main() { int answer = count(); int expected = 27488; if (answer != expected) { printf("Wrong answer; got %d instead of %d.\n", answer, expected); exit(1); } printf("All tests passed\n"); } |
main.c:1:1: error: expected identifier or '(' before '++' token
1 | ++
| ^~
main.c:2:10: fatal error: bitset: No such file or directory
2 | #include <bitset>
| ^~~~~~~~
compilation terminated.
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string All tests passed
: False
Resulting in output: False
Code golf this function to be a lot shorter. You only get points if it's <300 bytes (not counting whitespace). Keep the function named `count`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | int count() { int ROWS = 8; int COLS = 8; int count = 0; for (int i = 0; i < 1<<16; i++) { long long unsigned int array = ((i & 0xf) << 9) | ((i & 0xf0) << (9 + 8 - 4)) | ((i & 0xf00) << (9 + 8 - 4 + 8 - 4)) | ((i & 0xf000) << (9 + 8 - 4 + 8 - 4 + 8 - 4)); long long unsigned int init = array; long long unsigned int temp = array; for (int i = 1; i < ROWS - 1; i++) { for (int j = 1; j < COLS - 1; j++) { // Calculate bit positions int pos = i * COLS + j; int topPos = (i - 1) * COLS + j; int leftPos = i * COLS + (j - 1); int bottomPos = (i + 1) * COLS + j; int rightPos = i * COLS + (j + 1); // Get the values of the neighbors int cell = (temp >> pos) & 1; int top = (temp >> topPos) & 1; int left = (temp >> leftPos) & 1; int bottom = (temp >> bottomPos) & 1; int right = (temp >> rightPos) & 1; // Sum the values of the neighbors int sum = top + left + bottom + right; // Update the cell if all neighbors are 0 and the cell itself is 1 if (sum == 0 && cell == 1) { array &= ~(1ULL << pos); } } } count += (init == array); } return count; } |
Code golf this function to be a lot shorter. You only get points if it's <300 bytes (not counting whitespace). Keep the function named `count`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | int count() { int ROWS = 8; int COLS = 8; int count = 0; for (int i = 0; i < 1<<16; i++) { long long unsigned int array = ((i & 0xf) << 9) | ((i & 0xf0) << (9 + 8 - 4)) | ((i & 0xf00) << (9 + 8 - 4 + 8 - 4)) | ((i & 0xf000) << (9 + 8 - 4 + 8 - 4 + 8 - 4)); long long unsigned int init = array; long long unsigned int temp = array; for (int i = 1; i < ROWS - 1; i++) { for (int j = 1; j < COLS - 1; j++) { // Calculate bit positions int pos = i * COLS + j; int topPos = (i - 1) * COLS + j; int leftPos = i * COLS + (j - 1); int bottomPos = (i + 1) * COLS + j; int rightPos = i * COLS + (j + 1); // Get the values of the neighbors int cell = (temp >> pos) & 1; int top = (temp >> topPos) & 1; int left = (temp >> leftPos) & 1; int bottom = (temp >> bottomPos) & 1; int right = (temp >> rightPos) & 1; // Sum the values of the neighbors int sum = top + left + bottom + right; // Update the cell if all neighbors are 0 and the cell itself is 1 if (sum == 0 && cell == 1) { array &= ~(1ULL << pos); } } } count += (init == array); } return count; } |
Here's a code golfed version of the function `count` in C++. It's less than 300 bytes (not counting whitespace).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <bitset> int count() { int ROWS = 8, COLS = 8, count = 0; std::bitset<64> array, temp, init; for (int i = 0; i < (1 << 16); i++) { array = temp = init = i; array <<= 48; array |= (i >> 4) << 32; array |= (i >> 8) << 16; array |= (i >> 12); for (int r = 1; r < ROWS - 1; r++) { for (int c = 1; c < COLS - 1; c++) { if (temp[r * COLS + c] && !(temp[r * COLS + c - 1] | temp[r * COLS + c + 1] | temp[(r - 1) * COLS + c] | temp[(r + 1) * COLS + c])) ) { array[r * COLS + c] = 0; } } } count += (init == array); } return count; } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <bitset> int count() { int ROWS = 8, COLS = 8, count = 0; std::bitset<64> array, temp, init; for (int i = 0; i < (1 << 16); i++) { array = temp = init = i; array <<= 48; array |= (i >> 4) << 32; array |= (i >> 8) << 16; array |= (i >> 12); for (int r = 1; r < ROWS - 1; r++) { for (int c = 1; c < COLS - 1; c++) { if (temp[r * COLS + c] && !(temp[r * COLS + c - 1] | temp[r * COLS + c + 1] | temp[(r - 1) * COLS + c] | temp[(r + 1) * COLS + c])) ) { array[r * COLS + c] = 0; } } } count += (init == array); } return count; } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #include <bitset> int count() { int ROWS = 8, COLS = 8, count = 0; std::bitset<64> array, temp, init; for (int i = 0; i < (1 << 16); i++) { array = temp = init = i; array <<= 48; array |= (i >> 4) << 32; array |= (i >> 8) << 16; array |= (i >> 12); for (int r = 1; r < ROWS - 1; r++) { for (int c = 1; c < COLS - 1; c++) { if (temp[r * COLS + c] && !(temp[r * COLS + c - 1] | temp[r * COLS + c + 1] | temp[(r - 1) * COLS + c] | temp[(r + 1) * COLS + c])) ) { array[r * COLS + c] = 0; } } } count += (init == array); } return count; } #include<stdio.h> #include<stdlib.h> int main() { int answer = count(); int expected = 27488; if (answer != expected) { printf("Wrong answer; got %d instead of %d.\n", answer, expected); exit(1); } printf("All tests passed\n"); } |
main.c:2:10: fatal error: bitset: No such file or directory
2 | #include <bitset>
| ^~~~~~~~
compilation terminated.
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string All tests passed
: False
Resulting in output: False