Code golf this function to be a lot shorter. You only get points if it's <200 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 <200 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; } |
One acceptable answer was -------------------------------------------------- int count(){int i,s=0,r,c,p;unsigned long long a,b;for(i=0;i<1<<16;i++){a=(i&15)<<9|(i&240)<<13|(i&3840)<<17|(i&61440)<<21;b=a;for(r=1;r<7;r++)for(c=1;c<7;c++){p=r*8+c;if((a>>p)&1&&!((a>>(p-8)&1)|(a>>(p-1)&1)|(a>>(p+8)&1)|(a>>(p+1)&1)))a&=~(1ULL<<p);}s+=a==b;}return s;} -------------------------------------------------- Explanation: 1. We use 8×8 so the inner loops range from r=1 to 6 and c=1 to 6. 2. In the outer loop, i runs through 0…(1<<16)-1 and we “spread” the 16 bits into our board via a=(i&15)<<9 | (i&240)<<13 | (i&3840)<<17 | (i&61440)<<21. 3. We copy a into b for later comparison. Then the two nested loops check each cell. 4. For each (r,c) we compute p=r*8+c. If the cell is 1 and all of its four neighbors (above, left, below, right) are 0, we clear that cell. 5. After the update, if a equals b, we increment s. 6. Finally, we return s. Any solution meeting the criteria (<200 bytes, ignoring whitespace) is acceptable.
I extracted the following code from that output:
1 2 3 | #include <stdio.h> int count(){int i,s=0,r,c,p;unsigned long long a,b;for(i=0;i<1<<16;i++){a=(i&15)<<9|(i&240)<<13|(i&3840)<<17|(i&61440)<<21;b=a;for(r=1;r<7;r++)for(c=1;c<7;c++){p=r*8+c;if((a>>p)&1&&!((a>>(p-8)&1)|(a>>(p-1)&1)|(a>>(p+8)&1)|(a>>(p+1)&1)))a&=~(1ULL<<p);}s+=a==b;}return s;} |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <stdio.h> int count(){int i,s=0,r,c,p;unsigned long long a,b;for(i=0;i<1<<16;i++){a=(i&15)<<9|(i&240)<<13|(i&3840)<<17|(i&61440)<<21;b=a;for(r=1;r<7;r++)for(c=1;c<7;c++){p=r*8+c;if((a>>p)&1&&!((a>>(p-8)&1)|(a>>(p-1)&1)|(a>>(p+8)&1)|(a>>(p+1)&1)))a&=~(1ULL<<p);}s+=a==b;}return s;} #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"); } |
All tests passed
Testing if the previous output contains the string All tests passed
: True
Resulting in output: False
Code golf this function to be a lot shorter. You only get points if it's <200 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 <200 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; } |
One acceptable solution was -------------------------------------------------- int count(){int c=0;for(int i=0;i<1<<16;i++){unsigned long long u=((i&15)<<9)|((i&240)<<13)|((i&3840)<<17)|((i&61440)<<21),v=u;for(int r=1;r<7;r++)for(int s=1;s<7;s++){int p=r*8+s;if(v>>p&1&&!(((v>>(p-8))|(v>>(p-1))|(v>>(p+8))|(v>>(p+1)))&1))u&=~(1ULL<<p);}c+=u==v;}return c;} -------------------------------------------------- Explanation: 1. The 16‐bit loop (for(int i=0;i<1<<16;i++)) iterates over every possible 16–bit number. 2. We “spread” the 4 nibbles into a 64–bit board (the bit positions correspond to a row–major 8×8 grid) by shifting • (i&15) by 9, • (i&240) by 13, • (i&3840) by 17, • (i&61440) by 21. 3. We then run two nested loops (r from 1 to 6, s from 1 to 6) over the “inner” cells. 4. For each cell at position p=r*8+s we check if the cell is on (v>>p&1) and that none of its four direct neighbors is on (by OR–ing the neighbor bits and then ANDing with 1). 5. If so, we clear that bit in u. 6. Finally we compare the modified board with the original. If they’re equal we increment c. Any solution that does the same work and is under 200 bytes (ignoring whitespace) is acceptable.
I extracted the following code from that output:
1 2 3 | #include <stdio.h> int count(){int c=0;for(int i=0;i<1<<16;i++){unsigned long long u=((i&15)<<9)|((i&240)<<13)|((i&3840)<<17)|((i&61440)<<21),v=u;for(int r=1;r<7;r++)for(int s=1;s<7;s++){int p=r*8+s;if(v>>p&1&&!(((v>>(p-8))|(v>>(p-1))|(v>>(p+8))|(v>>(p+1)))&1))u&=~(1ULL<<p);}c+=u==v;}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 <stdio.h> int count(){int c=0;for(int i=0;i<1<<16;i++){unsigned long long u=((i&15)<<9)|((i&240)<<13)|((i&3840)<<17)|((i&61440)<<21),v=u;for(int r=1;r<7;r++)for(int s=1;s<7;s++){int p=r*8+s;if(v>>p&1&&!(((v>>(p-8))|(v>>(p-1))|(v>>(p+8))|(v>>(p+1)))&1))u&=~(1ULL<<p);}c+=u==v;}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"); } |
All tests passed
Testing if the previous output contains the string All tests passed
: True
Resulting in output: False