A binary 8x8 matrix is 'unholy' if there are no isolated 1s. A 1 is isolated if there isn't another 1 in any of the 4 direct cardinal directions. Write a C program that counts the total number of unholy 8x8 matricies.To solve this question: - Use bit arithmetic and an unsigned long to represent the matrix. - Write a function is_unholy that tests if a matrix is unholy by bit-shifting and masking. Use a dynamic program to count the number of unholy matricies. - Recurse on the number of rows the matrix has. - Enumerate all 2^16 next two rows. This is enough to completely isolate the prior solutions from the next. Write out a plan for the program, and then implement the plan in C.
A binary 8x8 matrix is 'unholy' if there are no isolated 1s. A 1 is isolated if there isn't another 1 in any of the 4 direct cardinal directions. Write a C program that counts the total number of unholy 8x8 matricies.To solve this question: - Use bit arithmetic and an unsigned long to represent the matrix. - Write a function is_unholy that tests if a matrix is unholy by bit-shifting and masking. Use a dynamic program to count the number of unholy matricies. - Recurse on the number of rows the matrix has. - Enumerate all 2^16 next two rows. This is enough to completely isolate the prior solutions from the next. Write out a plan for the program, and then implement the plan in C.
Program plan: 1. Define constants and data structures. 2. Write a function to convert a binary matrix represented as an unsigned long to an 8x8 2D array for debugging purposes. 3. Write a function to check if a matrix is unholy by checking the cardinal directions using bitwise operations. 4. Write a helper function to update the matrix by appending the next two rows. 5. Write the main dynamic programming function to count the number of unholy matrices. 6. Implement the main function to call the dynamic programming function and print the result. Now, let's implement the program in C:
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | #include <stdio.h> #include <stdint.h> #include <string.h> #define SIZE 8 #define MASK ((1ULL << (SIZE * SIZE)) - 1) void print_matrix(unsigned long matrix); int is_unholy(unsigned long matrix); void update_matrix(unsigned long *matrix, uint64_t value); unsigned long count_unholy(int rows); int main() { printf("Number of unholy %dx%d matrices: %lu\n", SIZE, SIZE, count_unholy(SIZE)); return 0; } void print_matrix(unsigned long matrix) { for (int i = 0; i < SIZE; ++i) { for (int j = 0; j < SIZE; ++j) { putchar(((matrix >> (i * SIZE + j)) & 1) ? '1' : '0'); } putchar('\n'); } } int is_unholy(unsigned long matrix) { unsigned long up = matrix >> SIZE; unsigned long down = matrix << SIZE; unsigned long right = matrix << 1; unsigned long left = matrix >> 1; unsigned long temp; // Check the top row temp = matrix & (~(left | right)); if (temp & (temp - 1)) { return 0; } // Check the middle rows for (int i = 1; i < SIZE - 1; ++i) { temp = ((matrix >> (i * SIZE)) & MASK) & (~(up | down | left | right)); if (temp & (temp - 1)) { return 0; } } // Check the bottom row temp = (matrix << (SIZE * (SIZE - 1))) & MASK; temp &= ~(up | left | right); if (temp & (temp - 1)) { return 0; } return 1; } void update_matrix(unsigned long *matrix, uint64_t value) { *matrix = (*matrix << (SIZE * SIZE * 2)) | (value & MASK) | ((value & MASK) << (SIZE * SIZE)); } unsigned long count_unholy(int rows) { unsigned long count = 0; if (rows == 0) { return 1; } unsigned long prev = count_unholy(rows - 2); for (uint64_t curr = 0; curr < (1ULL << (SIZE * SIZE)); ++curr) { if (is_unholy(curr)) { count += prev; } update_matrix(&prev, curr); } 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | #include <stdio.h> #include <stdint.h> #include <string.h> #define SIZE 8 #define MASK ((1ULL << (SIZE * SIZE)) - 1) void print_matrix(unsigned long matrix); int is_unholy(unsigned long matrix); void update_matrix(unsigned long *matrix, uint64_t value); unsigned long count_unholy(int rows); int main() { printf("Number of unholy %dx%d matrices: %lu\n", SIZE, SIZE, count_unholy(SIZE)); return 0; } void print_matrix(unsigned long matrix) { for (int i = 0; i < SIZE; ++i) { for (int j = 0; j < SIZE; ++j) { putchar(((matrix >> (i * SIZE + j)) & 1) ? '1' : '0'); } putchar('\n'); } } int is_unholy(unsigned long matrix) { unsigned long up = matrix >> SIZE; unsigned long down = matrix << SIZE; unsigned long right = matrix << 1; unsigned long left = matrix >> 1; unsigned long temp; // Check the top row temp = matrix & (~(left | right)); if (temp & (temp - 1)) { return 0; } // Check the middle rows for (int i = 1; i < SIZE - 1; ++i) { temp = ((matrix >> (i * SIZE)) & MASK) & (~(up | down | left | right)); if (temp & (temp - 1)) { return 0; } } // Check the bottom row temp = (matrix << (SIZE * (SIZE - 1))) & MASK; temp &= ~(up | left | right); if (temp & (temp - 1)) { return 0; } return 1; } void update_matrix(unsigned long *matrix, uint64_t value) { *matrix = (*matrix << (SIZE * SIZE * 2)) | (value & MASK) | ((value & MASK) << (SIZE * SIZE)); } unsigned long count_unholy(int rows) { unsigned long count = 0; if (rows == 0) { return 1; } unsigned long prev = count_unholy(rows - 2); for (uint64_t curr = 0; curr < (1ULL << (SIZE * SIZE)); ++curr) { if (is_unholy(curr)) { count += prev; } update_matrix(&prev, curr); } 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | #include <stdio.h> #include <stdint.h> #include <string.h> #define SIZE 8 #define MASK ((1ULL << (SIZE * SIZE)) - 1) void print_matrix(unsigned long matrix); int is_unholy(unsigned long matrix); void update_matrix(unsigned long *matrix, uint64_t value); unsigned long count_unholy(int rows); int main() { printf("Number of unholy %dx%d matrices: %lu\n", SIZE, SIZE, count_unholy(SIZE)); return 0; } void print_matrix(unsigned long matrix) { for (int i = 0; i < SIZE; ++i) { for (int j = 0; j < SIZE; ++j) { putchar(((matrix >> (i * SIZE + j)) & 1) ? '1' : '0'); } putchar('\n'); } } int is_unholy(unsigned long matrix) { unsigned long up = matrix >> SIZE; unsigned long down = matrix << SIZE; unsigned long right = matrix << 1; unsigned long left = matrix >> 1; unsigned long temp; // Check the top row temp = matrix & (~(left | right)); if (temp & (temp - 1)) { return 0; } // Check the middle rows for (int i = 1; i < SIZE - 1; ++i) { temp = ((matrix >> (i * SIZE)) & MASK) & (~(up | down | left | right)); if (temp & (temp - 1)) { return 0; } } // Check the bottom row temp = (matrix << (SIZE * (SIZE - 1))) & MASK; temp &= ~(up | left | right); if (temp & (temp - 1)) { return 0; } return 1; } void update_matrix(unsigned long *matrix, uint64_t value) { *matrix = (*matrix << (SIZE * SIZE * 2)) | (value & MASK) | ((value & MASK) << (SIZE * SIZE)); } unsigned long count_unholy(int rows) { unsigned long count = 0; if (rows == 0) { return 1; } unsigned long prev = count_unholy(rows - 2); for (uint64_t curr = 0; curr < (1ULL << (SIZE * SIZE)); ++curr) { if (is_unholy(curr)) { count += prev; } update_matrix(&prev, curr); } return count; } |
Number of unholy 8x8 matrices: 0
main.c: In function 'is_unholy':
main.c:7:21: warning: left shift count >= width of type [-Wshift-count-overflow]
7 | #define MASK ((1ULL << (SIZE * SIZE)) - 1)
| ^~
main.c:43:42: note: in expansion of macro 'MASK'
43 | temp = ((matrix >> (i * SIZE)) & MASK)
| ^~~~
main.c:7:21: warning: left shift count >= width of type [-Wshift-count-overflow]
7 | #define MASK ((1ULL << (SIZE * SIZE)) - 1)
| ^~
main.c:51:46: note: in expansion of macro 'MASK'
51 | temp = (matrix << (SIZE * (SIZE - 1))) & MASK;
| ^~~~
main.c: In function 'update_matrix':
main.c:61:24: warning: left shift count >= width of type [-Wshift-count-overflow]
61 | *matrix = (*matrix << (SIZE * SIZE * 2)) | (value & MASK) | ((value & MASK) << (SIZE * SIZE));
| ^~
main.c:7:21: warning: left shift count >= width of type [-Wshift-count-overflow]
7 | #define MASK ((1ULL << (SIZE * SIZE)) - 1)
| ^~
main.c:61:57: note: in expansion of macro 'MASK'
61 | *matrix = (*matrix << (SIZE * SIZE * 2)) | (value & MASK) | ((value & MASK) << (SIZE * SIZE));
| ^~~~
main.c:7:21: warning: left shift count >= width of type [-Wshift-count-overflow]
7 | #define MASK ((1ULL << (SIZE * SIZE)) - 1)
| ^~
main.c:61:75: note: in expansion of macro 'MASK'
61 | *matrix = (*matrix << (SIZE * SIZE * 2)) | (value & MASK) | ((value & MASK) << (SIZE * SIZE));
| ^~~~
main.c:61:81: warning: left shift count >= width of type [-Wshift-count-overflow]
61 | *matrix = (*matrix << (SIZE * SIZE * 2)) | (value & MASK) | ((value & MASK) << (SIZE * SIZE));
| ^~
main.c: In function 'count_unholy':
main.c:72:42: warning: left shift count >= width of type [-Wshift-count-overflow]
72 | for (uint64_t curr = 0; curr < (1ULL << (SIZE * SIZE)); ++curr) {
| ^~
Testing if the previous output contains the string 1121231537486377866
: False
A binary 8x8 matrix is 'unholy' if there are no isolated 1s. A 1 is isolated if there isn't another 1 in any of the 4 direct cardinal directions. Write a C program that counts the total number of unholy 8x8 matricies.To solve this question: - Use bit arithmetic and an unsigned long to represent the matrix. - Write a function is_unholy that tests if a matrix is unholy by bit-shifting and masking. Use a dynamic program to count the number of unholy matricies. - Recurse on the number of rows the matrix has. - Enumerate all 2^16 next two rows. This is enough to completely isolate the prior solutions from the next. Write out a plan for the program, and then implement the plan in C.
A binary 8x8 matrix is 'unholy' if there are no isolated 1s. A 1 is isolated if there isn't another 1 in any of the 4 direct cardinal directions. Write a C program that counts the total number of unholy 8x8 matricies.To solve this question: - Use bit arithmetic and an unsigned long to represent the matrix. - Write a function is_unholy that tests if a matrix is unholy by bit-shifting and masking. Use a dynamic program to count the number of unholy matricies. - Recurse on the number of rows the matrix has. - Enumerate all 2^16 next two rows. This is enough to completely isolate the prior solutions from the next. Write out a plan for the program, and then implement the plan in C.
Plan: 1. Define a function `is_unholy` that checks if a matrix represented by an unsigned long is unholy. This function will shift and mask the bits to check for isolated 1s. 2. Define a function `count_unholy` that recursively counts the number of unholy matrices using dynamic programming. This function will take the number of rows in the matrix as an argument and use bitmasking to iterate over all possible combinations of the next two rows. 3. Initialize an array of size 9 to store the number of unholy matrices for each number of rows from 0 to 8. 4. Call the `count_unholy` function with 8 as the argument to count the number of unholy 8x8 matrices. 5. Print the result. Code:
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 46 | #include <stdio.h> #define N 8 #define MASK 0xFFFF // checks if a matrix is unholy int is_unholy(unsigned long matrix) { unsigned long prev = matrix & 0x8000000000000000ULL; matrix <<= 1; while (matrix) { unsigned long curr = matrix & 0x8000000000000000ULL; if (curr && !(prev || (matrix & 0x4000000000000000ULL) || (matrix & 0x2000000000000000ULL) || (matrix & 0x1000000000000000ULL))) { return 0; } prev = curr; matrix <<= 1; } return 1; } // recursively counts the number of unholy matrices using dynamic programming unsigned long count_unholy(int rows) { static unsigned long cache[N + 1]; if (rows == 0 || rows == 1) { return 1; } if (rows >= N) { return cache[rows]; } unsigned long count = 0; for (unsigned long i = 0; i < 0x10000; i++) { for (unsigned long j = 0; j < 0x10000; j++) { unsigned long matrix = ((unsigned long)i << 32) | j; if (is_unholy(matrix) && count_unholy(rows - 2)) { count++; } } } cache[rows] = count; return count; } int main() { printf("%lu\n", count_unholy(N)); return 0; } |
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #include <stdio.h> #define N 8 #define MASK 0xFFFF // checks if a matrix is unholy int is_unholy(unsigned long matrix) { unsigned long prev = matrix & 0x8000000000000000ULL; matrix <<= 1; while (matrix) { unsigned long curr = matrix & 0x8000000000000000ULL; if (curr && !(prev || (matrix & 0x4000000000000000ULL) || (matrix & 0x2000000000000000ULL) || (matrix & 0x1000000000000000ULL))) { return 0; } prev = curr; matrix <<= 1; } return 1; } // recursively counts the number of unholy matrices using dynamic programming unsigned long count_unholy(int rows) { static unsigned long cache[N + 1]; if (rows == 0 || rows == 1) { return 1; } if (rows >= N) { return cache[rows]; } unsigned long count = 0; for (unsigned long i = 0; i < 0x10000; i++) { for (unsigned long j = 0; j < 0x10000; j++) { unsigned long matrix = ((unsigned long)i << 32) | j; if (is_unholy(matrix) && count_unholy(rows - 2)) { count++; } } } cache[rows] = count; return count; } int main() { printf("%lu\n", count_unholy(N)); return 0; } |
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 46 | #include <stdio.h> #define N 8 #define MASK 0xFFFF // checks if a matrix is unholy int is_unholy(unsigned long matrix) { unsigned long prev = matrix & 0x8000000000000000ULL; matrix <<= 1; while (matrix) { unsigned long curr = matrix & 0x8000000000000000ULL; if (curr && !(prev || (matrix & 0x4000000000000000ULL) || (matrix & 0x2000000000000000ULL) || (matrix & 0x1000000000000000ULL))) { return 0; } prev = curr; matrix <<= 1; } return 1; } // recursively counts the number of unholy matrices using dynamic programming unsigned long count_unholy(int rows) { static unsigned long cache[N + 1]; if (rows == 0 || rows == 1) { return 1; } if (rows >= N) { return cache[rows]; } unsigned long count = 0; for (unsigned long i = 0; i < 0x10000; i++) { for (unsigned long j = 0; j < 0x10000; j++) { unsigned long matrix = ((unsigned long)i << 32) | j; if (is_unholy(matrix) && count_unholy(rows - 2)) { count++; } } } cache[rows] = count; return count; } int main() { printf("%lu\n", count_unholy(N)); return 0; } |
0
Testing if the previous output contains the string 1121231537486377866
: False
A binary 8x8 matrix is 'unholy' if there are no isolated 1s. A 1 is isolated if there isn't another 1 in any of the 4 direct cardinal directions. Write a C program that counts the total number of unholy 8x8 matricies.To solve this question: - Use bit arithmetic and an unsigned long to represent the matrix. - Write a function is_unholy that tests if a matrix is unholy by bit-shifting and masking. Use a dynamic program to count the number of unholy matricies. - Recurse on the number of rows the matrix has. - Enumerate all 2^16 next two rows. This is enough to completely isolate the prior solutions from the next. Write out a plan for the program, and then implement the plan in C.
A binary 8x8 matrix is 'unholy' if there are no isolated 1s. A 1 is isolated if there isn't another 1 in any of the 4 direct cardinal directions. Write a C program that counts the total number of unholy 8x8 matricies.To solve this question: - Use bit arithmetic and an unsigned long to represent the matrix. - Write a function is_unholy that tests if a matrix is unholy by bit-shifting and masking. Use a dynamic program to count the number of unholy matricies. - Recurse on the number of rows the matrix has. - Enumerate all 2^16 next two rows. This is enough to completely isolate the prior solutions from the next. Write out a plan for the program, and then implement the plan in C.
Here's a high-level plan for the C program: 1. Define a function `is_unholy` that checks if a binary matrix is unholy by using bit-shifting and masking. 2. Define a dynamic programming function `count_unholy_matrices` that calculates the number of unholy 8x8 matrices. 3. Initialize a 2D array `dp` to store the number of unholy matrices for a given number of rows and the last two rows. 4. Iterate through all possible combinations of the last two rows (2^16 combinations). 5. For each combination, recursively calculate the number of unholy matrices with `n` rows, given the last two rows. 6. Store the result in the `dp` array. 7. After filling the `dp` array, return the value in `dp[8][i]` for all possible `i`. 8. Finally, sum all the values in `dp[8]` to get the total number of unholy 8x8 matrices. Now, let's implement this plan in C:
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 <stdio.h> #include <stdint.h> // Check if a matrix is unholy int is_unholy(uint64_t matrix) { // Check for isolated 1s return ((matrix & (matrix >> 1)) | ((matrix & (matrix >> 8)) >> 1)) == 0; } // Calculate the number of unholy matrices with n rows, given the last two rows uint64_t count_unholy_matrices(int n, uint64_t last_two_rows, uint64_t (*dp)[16]) { if (n == 0) return 1; if (dp[n][last_two_rows]) return dp[n][last_two_rows]; uint64_t count = 0; for (uint64_t row = 0; row < 16; row++) { // Check if adding the row will create an unholy matrix if (is_unholy((last_two_rows << 8) | (last_two_rows >> 56) | row)) count += count_unholy_matrices(n - 1, row, dp); } // Memoize the result dp[n][last_two_rows] = count; return count; } int main() { uint64_t dp[9][16]; // 9 rows, including initial row of 0, and 16 possible last two rows for (int i = 0; i < 9; i++) { for (int j = 0; j < 16; j++) { dp[i][j] = 0; } } uint64_t total = 0; // Enumerate all 2^16 possible last two rows for (uint64_t last_two_rows = 0; last_two_rows < 16; last_two_rows++) { total += count_unholy_matrices(8, last_two_rows, dp); } printf("The total number of unholy 8x8 matrices is: %llu\n", total); return 0; } |
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include <stdio.h> #include <stdint.h> // Check if a matrix is unholy int is_unholy(uint64_t matrix) { // Check for isolated 1s return ((matrix & (matrix >> 1)) | ((matrix & (matrix >> 8)) >> 1)) == 0; } // Calculate the number of unholy matrices with n rows, given the last two rows uint64_t count_unholy_matrices(int n, uint64_t last_two_rows, uint64_t (*dp)[16]) { if (n == 0) return 1; if (dp[n][last_two_rows]) return dp[n][last_two_rows]; uint64_t count = 0; for (uint64_t row = 0; row < 16; row++) { // Check if adding the row will create an unholy matrix if (is_unholy((last_two_rows << 8) | (last_two_rows >> 56) | row)) count += count_unholy_matrices(n - 1, row, dp); } // Memoize the result dp[n][last_two_rows] = count; return count; } int main() { uint64_t dp[9][16]; // 9 rows, including initial row of 0, and 16 possible last two rows for (int i = 0; i < 9; i++) { for (int j = 0; j < 16; j++) { dp[i][j] = 0; } } uint64_t total = 0; // Enumerate all 2^16 possible last two rows for (uint64_t last_two_rows = 0; last_two_rows < 16; last_two_rows++) { total += count_unholy_matrices(8, last_two_rows, dp); } printf("The total number of unholy 8x8 matrices is: %llu\n", total); return 0; } |
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 <stdio.h> #include <stdint.h> // Check if a matrix is unholy int is_unholy(uint64_t matrix) { // Check for isolated 1s return ((matrix & (matrix >> 1)) | ((matrix & (matrix >> 8)) >> 1)) == 0; } // Calculate the number of unholy matrices with n rows, given the last two rows uint64_t count_unholy_matrices(int n, uint64_t last_two_rows, uint64_t (*dp)[16]) { if (n == 0) return 1; if (dp[n][last_two_rows]) return dp[n][last_two_rows]; uint64_t count = 0; for (uint64_t row = 0; row < 16; row++) { // Check if adding the row will create an unholy matrix if (is_unholy((last_two_rows << 8) | (last_two_rows >> 56) | row)) count += count_unholy_matrices(n - 1, row, dp); } // Memoize the result dp[n][last_two_rows] = count; return count; } int main() { uint64_t dp[9][16]; // 9 rows, including initial row of 0, and 16 possible last two rows for (int i = 0; i < 9; i++) { for (int j = 0; j < 16; j++) { dp[i][j] = 0; } } uint64_t total = 0; // Enumerate all 2^16 possible last two rows for (uint64_t last_two_rows = 0; last_two_rows < 16; last_two_rows++) { total += count_unholy_matrices(8, last_two_rows, dp); } printf("The total number of unholy 8x8 matrices is: %llu\n", total); return 0; } |
The total number of unholy 8x8 matrices is: 17792272
main.c: In function 'main':
main.c:44:60: warning: format '%llu' expects argument of type 'long long unsigned int', but argument 2 has type 'uint64_t' {aka 'long unsigned int'} [-Wformat=]
44 | printf("The total number of unholy 8x8 matrices is: %llu\n", total);
| ~~~^ ~~~~~
| | |
| | uint64_t {aka long unsigned int}
| long long unsigned int
| %lu
Testing if the previous output contains the string 1121231537486377866
: False
A binary 8x8 matrix is 'unholy' if there are no isolated 1s. A 1 is isolated if there isn't another 1 in any of the 4 direct cardinal directions. Write a C program that counts the total number of unholy 8x8 matricies.To solve this question: - Use bit arithmetic and an unsigned long to represent the matrix. - Write a function is_unholy that tests if a matrix is unholy by bit-shifting and masking. Use a dynamic program to count the number of unholy matricies. - Recurse on the number of rows the matrix has. - Enumerate all 2^16 next two rows. This is enough to completely isolate the prior solutions from the next. Write out a plan for the program, and then implement the plan in C.
A binary 8x8 matrix is 'unholy' if there are no isolated 1s. A 1 is isolated if there isn't another 1 in any of the 4 direct cardinal directions. Write a C program that counts the total number of unholy 8x8 matricies.To solve this question: - Use bit arithmetic and an unsigned long to represent the matrix. - Write a function is_unholy that tests if a matrix is unholy by bit-shifting and masking. Use a dynamic program to count the number of unholy matricies. - Recurse on the number of rows the matrix has. - Enumerate all 2^16 next two rows. This is enough to completely isolate the prior solutions from the next. Write out a plan for the program, and then implement the plan in C.
Here's a plan for the C program: 1. Define constants and data structures. 2. Create a function `is_unholy` to check if a matrix is unholy. 3. Create a function `count_unholy_helper` to recursively count the number of unholy matrices for a given number of rows. 4. Create a function `count_unholy` to count the total number of unholy 8x8 matrices. 5. Write the main function to call `count_unholy` and print the result. Now, let's implement the plan in C:
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 | #include <stdio.h> #include <stdint.h> #define SIZE 8 #define MASK (((uint64_t)1 << SIZE) - 1) // Function to check if a matrix is unholy int is_unholy(uint64_t a, uint64_t b) { uint64_t c = a | (b << SIZE); c |= (c >> 1) & MASK; c |= (c >> 2) & (MASK << 1); c |= (c >> 4) & (MASK << 4); c &= c << SIZE; return !(a & (c >> SIZE)) && !(b & c); } // Recursive function to count the number of unholy matrices for a given number of rows uint64_t count_unholy_helper(int rows) { if (rows == 0) return 1; uint64_t count = 0; for (uint64_t i = 0; i < (1 << (SIZE * 2)); ++i) { for (uint64_t j = 0; j < (1 << (SIZE * 2)); ++j) { if (is_unholy(i, j)) count += count_unholy_helper(rows - 1); } } return count; } // Function to count the total number of unholy 8x8 matrices uint64_t count_unholy() { return count_unholy_helper(SIZE); } int main() { uint64_t result = count_unholy(); printf("The total number of unholy 8x8 matrices is: %llu\n", result); return 0; } |
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 29 30 31 32 33 34 35 36 37 38 | #include <stdio.h> #include <stdint.h> #define SIZE 8 #define MASK (((uint64_t)1 << SIZE) - 1) // Function to check if a matrix is unholy int is_unholy(uint64_t a, uint64_t b) { uint64_t c = a | (b << SIZE); c |= (c >> 1) & MASK; c |= (c >> 2) & (MASK << 1); c |= (c >> 4) & (MASK << 4); c &= c << SIZE; return !(a & (c >> SIZE)) && !(b & c); } // Recursive function to count the number of unholy matrices for a given number of rows uint64_t count_unholy_helper(int rows) { if (rows == 0) return 1; uint64_t count = 0; for (uint64_t i = 0; i < (1 << (SIZE * 2)); ++i) { for (uint64_t j = 0; j < (1 << (SIZE * 2)); ++j) { if (is_unholy(i, j)) count += count_unholy_helper(rows - 1); } } return count; } // Function to count the total number of unholy 8x8 matrices uint64_t count_unholy() { return count_unholy_helper(SIZE); } int main() { uint64_t result = count_unholy(); printf("The total number of unholy 8x8 matrices is: %llu\n", result); return 0; } |
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 | #include <stdio.h> #include <stdint.h> #define SIZE 8 #define MASK (((uint64_t)1 << SIZE) - 1) // Function to check if a matrix is unholy int is_unholy(uint64_t a, uint64_t b) { uint64_t c = a | (b << SIZE); c |= (c >> 1) & MASK; c |= (c >> 2) & (MASK << 1); c |= (c >> 4) & (MASK << 4); c &= c << SIZE; return !(a & (c >> SIZE)) && !(b & c); } // Recursive function to count the number of unholy matrices for a given number of rows uint64_t count_unholy_helper(int rows) { if (rows == 0) return 1; uint64_t count = 0; for (uint64_t i = 0; i < (1 << (SIZE * 2)); ++i) { for (uint64_t j = 0; j < (1 << (SIZE * 2)); ++j) { if (is_unholy(i, j)) count += count_unholy_helper(rows - 1); } } return count; } // Function to count the total number of unholy 8x8 matrices uint64_t count_unholy() { return count_unholy_helper(SIZE); } int main() { uint64_t result = count_unholy(); printf("The total number of unholy 8x8 matrices is: %llu\n", result); return 0; } |
Timeout: function took too long to complete
Testing if the previous output contains the string 1121231537486377866
: False
A binary 8x8 matrix is 'unholy' if there are no isolated 1s. A 1 is isolated if there isn't another 1 in any of the 4 direct cardinal directions. Write a C program that counts the total number of unholy 8x8 matricies.To solve this question: - Use bit arithmetic and an unsigned long to represent the matrix. - Write a function is_unholy that tests if a matrix is unholy by bit-shifting and masking. Use a dynamic program to count the number of unholy matricies. - Recurse on the number of rows the matrix has. - Enumerate all 2^16 next two rows. This is enough to completely isolate the prior solutions from the next. Write out a plan for the program, and then implement the plan in C.
A binary 8x8 matrix is 'unholy' if there are no isolated 1s. A 1 is isolated if there isn't another 1 in any of the 4 direct cardinal directions. Write a C program that counts the total number of unholy 8x8 matricies.To solve this question: - Use bit arithmetic and an unsigned long to represent the matrix. - Write a function is_unholy that tests if a matrix is unholy by bit-shifting and masking. Use a dynamic program to count the number of unholy matricies. - Recurse on the number of rows the matrix has. - Enumerate all 2^16 next two rows. This is enough to completely isolate the prior solutions from the next. Write out a plan for the program, and then implement the plan in C.
Plan for the program: 1. Define a function `is_unholy` that takes an unsigned long representing an 8x8 matrix and returns 1 if it is unholy, and 0 otherwise. The function should use bit-shifting and masking to test if there are any isolated 1s in the matrix. 2. Define a function `count_unholy_matrices` that takes an integer `num_rows` representing the number of rows in the matrix and returns the number of unholy 8x8 matrices with `num_rows` rows. The function should use a dynamic programming approach to count the number of unholy matrices. 3. In the `count_unholy_matrices` function, use a recursive approach to count the number of unholy matrices. The base case should be when `num_rows` is 0, in which case the function should return 1 (since there is 1 way to have an empty matrix). For other values of `num_rows`, the function should enumerate all 2^16 possible next two rows and use the `is_unholy` function to test if they are unholy. The function should then recursively call itself with `num_rows - 2` to count the number of unholy matrices that can follow the current rows. 4. In the main function, call the `count_unholy_matrices` function with `num_rows` set to 8 and print the result. Implementation of the plan in C:
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #include <stdio.h> #include <stdint.h> // Function to check if a matrix is unholy int is_unholy(uint64_t matrix) { uint64_t mask = 0x8888888888888888; uint64_t shifted; // Check for isolated 1s in each row mask &= matrix; while (mask) { shifted = mask << 1 | mask >> 7; if ((mask & 0x0101010101010101) && (mask != shifted)) { return 0; } mask = shifted; } // Check for isolated 1s in each column matrix = (matrix >> 1) | (matrix << 7); matrix &= 0x0202020202020202; mask = matrix; while (mask) { shifted = mask << 8 | mask >> 8; if ((mask & 0x0101010101010101) && (mask != shifted)) { return 0; } mask = shifted; } return 1; } // Function to count the number of unholy matrices with num_rows rows uint64_t count_unholy_matrices(int num_rows) { if (num_rows == 0) { return 1; } uint64_t count = 0; uint64_t prev_prev_rows, prev_rows; // Enumerate all 2^16 possible next two rows for (prev_rows = 0; prev_rows < 0x101010101010101; prev_rows++) { for (prev_prev_rows = 0; prev_prev_rows < 0x101010101010101; prev_prev_rows++) { // Check if the previous two rows and the current two rows form an unholy matrix uint64_t matrix = prev_prev_rows | (prev_rows << 64); if (is_unholy(matrix)) { // Count the number of unholy matrices that can follow the current rows count += count_unholy_matrices(num_rows - 2); } } } return count; } int main() { uint64_t num_unholy_matrices = count_unholy_matrices(8); printf("Number of unholy 8x8 matrices: %llu\n", num_unholy_matrices); return 0; } |
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #include <stdio.h> #include <stdint.h> // Function to check if a matrix is unholy int is_unholy(uint64_t matrix) { uint64_t mask = 0x8888888888888888; uint64_t shifted; // Check for isolated 1s in each row mask &= matrix; while (mask) { shifted = mask << 1 | mask >> 7; if ((mask & 0x0101010101010101) && (mask != shifted)) { return 0; } mask = shifted; } // Check for isolated 1s in each column matrix = (matrix >> 1) | (matrix << 7); matrix &= 0x0202020202020202; mask = matrix; while (mask) { shifted = mask << 8 | mask >> 8; if ((mask & 0x0101010101010101) && (mask != shifted)) { return 0; } mask = shifted; } return 1; } // Function to count the number of unholy matrices with num_rows rows uint64_t count_unholy_matrices(int num_rows) { if (num_rows == 0) { return 1; } uint64_t count = 0; uint64_t prev_prev_rows, prev_rows; // Enumerate all 2^16 possible next two rows for (prev_rows = 0; prev_rows < 0x101010101010101; prev_rows++) { for (prev_prev_rows = 0; prev_prev_rows < 0x101010101010101; prev_prev_rows++) { // Check if the previous two rows and the current two rows form an unholy matrix uint64_t matrix = prev_prev_rows | (prev_rows << 64); if (is_unholy(matrix)) { // Count the number of unholy matrices that can follow the current rows count += count_unholy_matrices(num_rows - 2); } } } return count; } int main() { uint64_t num_unholy_matrices = count_unholy_matrices(8); printf("Number of unholy 8x8 matrices: %llu\n", num_unholy_matrices); return 0; } |
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #include <stdio.h> #include <stdint.h> // Function to check if a matrix is unholy int is_unholy(uint64_t matrix) { uint64_t mask = 0x8888888888888888; uint64_t shifted; // Check for isolated 1s in each row mask &= matrix; while (mask) { shifted = mask << 1 | mask >> 7; if ((mask & 0x0101010101010101) && (mask != shifted)) { return 0; } mask = shifted; } // Check for isolated 1s in each column matrix = (matrix >> 1) | (matrix << 7); matrix &= 0x0202020202020202; mask = matrix; while (mask) { shifted = mask << 8 | mask >> 8; if ((mask & 0x0101010101010101) && (mask != shifted)) { return 0; } mask = shifted; } return 1; } // Function to count the number of unholy matrices with num_rows rows uint64_t count_unholy_matrices(int num_rows) { if (num_rows == 0) { return 1; } uint64_t count = 0; uint64_t prev_prev_rows, prev_rows; // Enumerate all 2^16 possible next two rows for (prev_rows = 0; prev_rows < 0x101010101010101; prev_rows++) { for (prev_prev_rows = 0; prev_prev_rows < 0x101010101010101; prev_prev_rows++) { // Check if the previous two rows and the current two rows form an unholy matrix uint64_t matrix = prev_prev_rows | (prev_rows << 64); if (is_unholy(matrix)) { // Count the number of unholy matrices that can follow the current rows count += count_unholy_matrices(num_rows - 2); } } } return count; } int main() { uint64_t num_unholy_matrices = count_unholy_matrices(8); printf("Number of unholy 8x8 matrices: %llu\n", num_unholy_matrices); return 0; } |
Timeout: function took too long to complete
Testing if the previous output contains the string 1121231537486377866
: False