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; } |
1. Explanation of the function: The function counts the number of 8x8 binary grids where cells with all zero neighbors remain unchanged after one iteration. It iterates through all possible 16-bit configurations, expands them to 8x8 grids, applies the rule, and counts cases where the grid doesn't change. 2. Ways to make it shorter: - Use bitwise operations instead of nested loops for neighbor checking - Eliminate separate variables for rows and columns - Combine operations to reduce the number of lines - Use more compact syntax for bitwise operations 3. Shortened version:
1 | int count(){int c=0,i,j;for(i=0;i<1<<16;i++){unsigned long long a=((i&15ULL)<<9)|((i&240ULL)<<13)|((i&3840ULL)<<17)|((i&61440ULL)<<21),t=a;for(j=9;j<55;j++)if((t>>j&1)&&!((t>>(j-9)&1)|(t>>(j-1)&1)|(t>>(j+1)&1)|(t>>(j+9)&1)))a&=~(1ULL<<j);c+=a==t;}return c;} |
1 | int count(){int c=0,i;for(i=0;i<1<<16;i++){unsigned long long a=((i&15ULL)<<9)|((i&240ULL)<<13)|((i&3840ULL)<<17)|((i&61440ULL)<<21),t=a,m;for(m=0x1FE00;m<0x1FE0000000ULL;m<<=9)a&=~(t&m&~((t>>9|t<<9|t>>1|t<<1)&m));c+=a==t;}return c;} |
I extracted the following code from that output:
1 2 3 | #include <stdio.h> int count(){int c=0,i;for(i=0;i<1<<16;i++){unsigned long long a=((i&15ULL)<<9)|((i&240ULL)<<13)|((i&3840ULL)<<17)|((i&61440ULL)<<21),t=a,m;for(m=0x1FE00;m<0x1FE0000000ULL;m<<=9)a&=~(t&m&~((t>>9|t<<9|t>>1|t<<1)&m));c+=a==t;}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 20 | #include <stdio.h> int count(){int c=0,i;for(i=0;i<1<<16;i++){unsigned long long a=((i&15ULL)<<9)|((i&240ULL)<<13)|((i&3840ULL)<<17)|((i&61440ULL)<<21),t=a,m;for(m=0x1FE00;m<0x1FE0000000ULL;m<<=9)a&=~(t&m&~((t>>9|t<<9|t>>1|t<<1)&m));c+=a==t;}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"); } |
Wrong answer; got 18536 instead of 27488.
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; } |
1. Explanation of the function: The function counts the number of 8x8 binary grids where no cell with all 0 neighbors changes from 1 to 0 after one iteration. It iterates through all possible 16-bit configurations, expands them to 8x8 grids, applies the rule, and counts unchanged grids. 2. Ways to make it shorter: - Use bitwise operations instead of explicit loops for neighbor checking - Combine the grid expansion and rule application into a single operation - Eliminate temporary variables and intermediate steps - Use a lookup table for faster computation of unchanged grids 3. Shorter version:
1 | int count(){int c=0,i;for(i=0;i<65536;i++)c+=(__builtin_popcountll((i&15)<<9|(i&240)<<13|(i&3840)<<17|(i&61440)<<21)&0x7e7e7e7e7e7e7e7e)==__builtin_popcountll(i));return c;} |
1 | int count(){int c=0,i;for(i=0;i<65536;i++)c+=__builtin_popcountll((i&15)<<9|(i&240)<<13|(i&3840)<<17|(i&61440)<<21)&0x7e7e7e7e7e7e7e7e==__builtin_popcountll(i);return c;} |
I extracted the following code from that output:
1 2 3 | #include <x86intrin.h> int count(){int c=0,i;for(i=0;i<65536;i++)c+=__builtin_popcountll((i&15)<<9|(i&240)<<13|(i&3840)<<17|(i&61440)<<21)&0x7e7e7e7e7e7e7e7e==__builtin_popcountll(i);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 20 | #include <x86intrin.h> int count(){int c=0,i;for(i=0;i<65536;i++)c+=__builtin_popcountll((i&15)<<9|(i&240)<<13|(i&3840)<<17|(i&61440)<<21)&0x7e7e7e7e7e7e7e7e==__builtin_popcountll(i);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"); } |
Wrong answer; got 0 instead of 27488.
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; } |
1. Explanation of the function: The function counts the number of 8x8 binary grids where no cell with all zero neighbors changes from 1 to 0 after one iteration. It iterates through all possible 16-bit configurations, expands them to 8x8 grids, applies the rule, and counts unchanged grids. 2. Ways to make the function shorter: - Use bitwise operations instead of explicit loops for neighbor checking - Combine the grid expansion and iteration into a single loop - Eliminate unnecessary variables and simplify calculations 3. Shortened version:
1 | int count(){int c=0,i,j,g;for(i=0;i<65536;i++){g=0;for(j=0;j<16;j++)g|=((i>>j)&1)<<(9*(j/4)+j%4);c+=g==((g&~0x7e7e7e7e7e7e7e7e)|((g&0x7e7e7e7e7e7e7e7e)&~((g>>1|g<<1|g>>8|g<<8)&0x7e7e7e7e7e7e7e7e)));}return c;} |
1 | int count(){int c=0,i,g;for(i=0;i<65536;i++){g=((i&15)<<9)|((i&240)<<13)|((i&3840)<<17)|((i&61440)<<21);c+=g==((g&~0x7e7e7e7e7e7e7e7e)|((g&0x7e7e7e7e7e7e7e7e)&~((g>>1|g<<1|g>>8|g<<8)&0x7e7e7e7e7e7e7e7e)));}return c;} |
I extracted the following code from that output:
1 2 3 | #include <stdio.h> int count(){int c=0,i,g;for(i=0;i<65536;i++){g=((i&15)<<9)|((i&240)<<13)|((i&3840)<<17)|((i&61440)<<21);c+=g==((g&~0x7e7e7e7e7e7e7e7e)|((g&0x7e7e7e7e7e7e7e7e)&~((g>>1|g<<1|g>>8|g<<8)&0x7e7e7e7e7e7e7e7e)));}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 20 | #include <stdio.h> int count(){int c=0,i,g;for(i=0;i<65536;i++){g=((i&15)<<9)|((i&240)<<13)|((i&3840)<<17)|((i&61440)<<21);c+=g==((g&~0x7e7e7e7e7e7e7e7e)|((g&0x7e7e7e7e7e7e7e7e)&~((g>>1|g<<1|g>>8|g<<8)&0x7e7e7e7e7e7e7e7e)));}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"); } |
Wrong answer; got 3632 instead of 27488.
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; } |
1. Explanation of the function: The function counts the number of 8x8 binary grids where the grid remains unchanged after applying a specific rule: a cell with value 1 changes to 0 if all its orthogonal neighbors are 0. 2. Ways to make the function shorter: - Use bitwise operations to check and update cells instead of explicit loops. - Combine the grid representation into a single 64-bit integer. - Eliminate unnecessary variables and calculations. - Use more compact syntax for bitwise operations. 3. Shorter version of the function:
1 | int count(){int c=0;for(unsigned long long i=0;i<1ULL<<64;i++){unsigned long long m=i&0x7e7e7e7e7e7e7e7eULL;c+=(i==(i&~((m<<1|m>>1|m<<8|m>>8)^0xffffffffffffffffULL)));}return c;} |
1 | int count(){int c=0;for(unsigned long long i=0;i<1ULL<<64;i++)c+=(i==(i&~((((i&0x7e7e7e7e7e7e7e7eULL)<<1|(i&0x7e7e7e7e7e7e7e7eULL)>>1|(i&0x7e7e7e7e7e7e7e7eULL)<<8|(i&0x7e7e7e7e7e7e7e7eULL)>>8)^-1ULL)));return c;} |
I extracted the following code from that output:
1 2 3 | #include <stdint.h> int count(){int c=0;for(unsigned long long i=0;i<1ULL<<64;i++)c+=(i==(i&~((((i&0x7e7e7e7e7e7e7e7eULL)<<1|(i&0x7e7e7e7e7e7e7e7eULL)>>1|(i&0x7e7e7e7e7e7e7e7eULL)<<8|(i&0x7e7e7e7e7e7e7e7eULL)>>8)^-1ULL)));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 20 | #include <stdint.h> int count(){int c=0;for(unsigned long long i=0;i<1ULL<<64;i++)c+=(i==(i&~((((i&0x7e7e7e7e7e7e7e7eULL)<<1|(i&0x7e7e7e7e7e7e7e7eULL)>>1|(i&0x7e7e7e7e7e7e7e7eULL)<<8|(i&0x7e7e7e7e7e7e7e7eULL)>>8)^-1ULL)));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 'count':
main.c:4:54: warning: left shift count >= width of type [-Wshift-count-overflow]
4 | int count(){int c=0;for(unsigned long long i=0;i<1ULL<<64;i++)c+=(i==(i&~((((i&0x7e7e7e7e7e7e7e7eULL)<<1|(i&0x7e7e7e7e7e7e7e7eULL)>>1|(i&0x7e7e7e7e7e7e7e7eULL)<<8|(i&0x7e7e7e7e7e7e7e7eULL)>>8)^-1ULL)));return c;}
| ^~
main.c:4:202: error: expected ')' before ';' token
4 | int count(){int c=0;for(unsigned long long i=0;i<1ULL<<64;i++)c+=(i==(i&~((((i&0x7e7e7e7e7e7e7e7eULL)<<1|(i&0x7e7e7e7e7e7e7e7eULL)>>1|(i&0x7e7e7e7e7e7e7e7eULL)<<8|(i&0x7e7e7e7e7e7e7e7eULL)>>8)^-1ULL)));return c;}
| ~ ^
| )
main.c:4:212: error: expected ';' before '}' token
4 | int count(){int c=0;for(unsigned long long i=0;i<1ULL<<64;i++)c+=(i==(i&~((((i&0x7e7e7e7e7e7e7e7eULL)<<1|(i&0x7e7e7e7e7e7e7e7eULL)>>1|(i&0x7e7e7e7e7e7e7e7eULL)<<8|(i&0x7e7e7e7e7e7e7e7eULL)>>8)^-1ULL)));return c;}
| ^
| ;
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; } |
1. Explanation of the function: The function counts the number of 8x8 binary grids where the inner 6x6 cells remain unchanged after applying a specific rule. The rule is: a cell with a value of 1 becomes 0 if all its neighbors (top, left, bottom, right) are 0. 2. Ways to make it shorter: - Use bitwise operations instead of loops for neighbor checks - Utilize the symmetry of the problem to reduce iterations - Combine multiple operations into single expressions - Use shorter variable names 3. Shortened version:
1 | int count(){int c=0,i;for(i=0;i<65536;i++){unsigned long long a=((unsigned long long)i<<9|i<<21|i<<33|i<<45)&0xFCFCFCFC3F3F3F3FULL,b=a&~((a>>9|a<<9|a>>1|a<<1|a)&~(a>>9|a<<9|a>>1|a<<1)&0x7E7E7E7E1C1C1C1CULL);c+=a==b;}return c;} |
1 | int count(){int c=0,i;for(i=0;i<65536;i++)c+=((unsigned long long)i<<9|i<<21|i<<33|i<<45)&0xFCFCFCFC3F3F3F3FULL)==(((unsigned long long)i<<9|i<<21|i<<33|i<<45)&0xFCFCFCFC3F3F3F3FULL&~((((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>1|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<1|((unsigned long long)i<<9|i<<21|i<<33|i<<45))&~(((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>1|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<1)&0x7E7E7E7E1C1C1C1CULL));return c;} |
I extracted the following code from that output:
1 2 3 | #include <stdint.h> int count(){int c=0,i;for(i=0;i<65536;i++)c+=((unsigned long long)i<<9|i<<21|i<<33|i<<45)&0xFCFCFCFC3F3F3F3FULL)==(((unsigned long long)i<<9|i<<21|i<<33|i<<45)&0xFCFCFCFC3F3F3F3FULL&~((((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>1|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<1|((unsigned long long)i<<9|i<<21|i<<33|i<<45))&~(((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>1|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<1)&0x7E7E7E7E1C1C1C1CULL));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 20 | #include <stdint.h> int count(){int c=0,i;for(i=0;i<65536;i++)c+=((unsigned long long)i<<9|i<<21|i<<33|i<<45)&0xFCFCFCFC3F3F3F3FULL)==(((unsigned long long)i<<9|i<<21|i<<33|i<<45)&0xFCFCFCFC3F3F3F3FULL&~((((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>1|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<1|((unsigned long long)i<<9|i<<21|i<<33|i<<45))&~(((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>1|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<1)&0x7E7E7E7E1C1C1C1CULL));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 'count':
main.c:4:79: warning: left shift count >= width of type [-Wshift-count-overflow]
4 | int count(){int c=0,i;for(i=0;i<65536;i++)c+=((unsigned long long)i<<9|i<<21|i<<33|i<<45)&0xFCFCFCFC3F3F3F3FULL)==(((unsigned long long)i<<9|i<<21|i<<33|i<<45)&0xFCFCFCFC3F3F3F3FULL&~((((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>1|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<1|((unsigned long long)i<<9|i<<21|i<<33|i<<45))&~(((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>1|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<1)&0x7E7E7E7E1C1C1C1CULL));return c;}
| ^~
main.c:4:85: warning: left shift count >= width of type [-Wshift-count-overflow]
4 | int count(){int c=0,i;for(i=0;i<65536;i++)c+=((unsigned long long)i<<9|i<<21|i<<33|i<<45)&0xFCFCFCFC3F3F3F3FULL)==(((unsigned long long)i<<9|i<<21|i<<33|i<<45)&0xFCFCFCFC3F3F3F3FULL&~((((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>1|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<1|((unsigned long long)i<<9|i<<21|i<<33|i<<45))&~(((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>1|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<1)&0x7E7E7E7E1C1C1C1CULL));return c;}
| ^~
main.c:4:112: error: expected ';' before ')' token
4 | int count(){int c=0,i;for(i=0;i<65536;i++)c+=((unsigned long long)i<<9|i<<21|i<<33|i<<45)&0xFCFCFCFC3F3F3F3FULL)==(((unsigned long long)i<<9|i<<21|i<<33|i<<45)&0xFCFCFCFC3F3F3F3FULL&~((((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>1|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<1|((unsigned long long)i<<9|i<<21|i<<33|i<<45))&~(((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>1|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<1)&0x7E7E7E7E1C1C1C1CULL));return c;}
| ^
| ;
main.c:4:112: error: expected statement before ')' token
main.c:4:113: error: expected expression before '==' token
4 | int count(){int c=0,i;for(i=0;i<65536;i++)c+=((unsigned long long)i<<9|i<<21|i<<33|i<<45)&0xFCFCFCFC3F3F3F3FULL)==(((unsigned long long)i<<9|i<<21|i<<33|i<<45)&0xFCFCFCFC3F3F3F3FULL&~((((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>1|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<1|((unsigned long long)i<<9|i<<21|i<<33|i<<45))&~(((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<9|((unsigned long long)i<<9|i<<21|i<<33|i<<45)>>1|((unsigned long long)i<<9|i<<21|i<<33|i<<45)<<1)&0x7E7E7E7E1C1C1C1CULL));return c;}
| ^~
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