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 the matrix is unholy, and 0 otherwise. - Use bit shifting and masking to check if each 1 in the matrix has at least one adjacent 1 in any of the 4 cardinal directions. 2. Define a function `count_unholy` that uses dynamic programming to count the number of unholy matrices. - The function will take the number of rows (0 to 8) and two unsigned longs representing the current and previous rows of the matrix. - Enumerate all possible configurations of the next two rows (2^16 possibilities) and recursively call `count_unholy` with the updated number of rows and the new current and previous rows. - Memoize the results to avoid redundant calculations. 3. In the `main` function, call `count_unholy` with initial parameters (0 rows, 0 for current and previous rows) to get the total count of unholy matrices. 4. 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 39 40 41 42 43 | #include <stdio.h> unsigned long memo[9][1 << 16]; int is_unholy(unsigned long matrix) { unsigned long mask = 0x8080808080808080UL; for (int i = 0; i < 8; i++) { unsigned long row = matrix & 0xFFUL; if ((row & (row << 1)) == 0 && (row & (row >> 1)) == 0 && (row & (matrix >> 8)) == 0 && (row & (matrix << 8)) == 0) { return 0; } matrix >>= 8; } return 1; } unsigned long count_unholy(int rows, unsigned long curr, unsigned long prev) { if (rows == 8) { return is_unholy((prev << 8) | curr); } unsigned long key = (prev << 8) | curr; if (memo[rows][key] != 0) { return memo[rows][key]; } unsigned long count = 0; for (unsigned long next = 0; next < (1 << 16); next++) { if (is_unholy((prev << 8) | curr | (next << 16))) { count += count_unholy(rows + 1, next >> 8, next & 0xFF); } } memo[rows][key] = count; return count; } int main() { unsigned long total_count = count_unholy(0, 0, 0); printf("Total number of unholy 8x8 matrices: %lu\n", total_count); 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 | #include <stdio.h> unsigned long memo[9][1 << 16]; int is_unholy(unsigned long matrix) { unsigned long mask = 0x8080808080808080UL; for (int i = 0; i < 8; i++) { unsigned long row = matrix & 0xFFUL; if ((row & (row << 1)) == 0 && (row & (row >> 1)) == 0 && (row & (matrix >> 8)) == 0 && (row & (matrix << 8)) == 0) { return 0; } matrix >>= 8; } return 1; } unsigned long count_unholy(int rows, unsigned long curr, unsigned long prev) { if (rows == 8) { return is_unholy((prev << 8) | curr); } unsigned long key = (prev << 8) | curr; if (memo[rows][key] != 0) { return memo[rows][key]; } unsigned long count = 0; for (unsigned long next = 0; next < (1 << 16); next++) { if (is_unholy((prev << 8) | curr | (next << 16))) { count += count_unholy(rows + 1, next >> 8, next & 0xFF); } } memo[rows][key] = count; return count; } int main() { unsigned long total_count = count_unholy(0, 0, 0); printf("Total number of unholy 8x8 matrices: %lu\n", total_count); 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 | #include <stdio.h> unsigned long memo[9][1 << 16]; int is_unholy(unsigned long matrix) { unsigned long mask = 0x8080808080808080UL; for (int i = 0; i < 8; i++) { unsigned long row = matrix & 0xFFUL; if ((row & (row << 1)) == 0 && (row & (row >> 1)) == 0 && (row & (matrix >> 8)) == 0 && (row & (matrix << 8)) == 0) { return 0; } matrix >>= 8; } return 1; } unsigned long count_unholy(int rows, unsigned long curr, unsigned long prev) { if (rows == 8) { return is_unholy((prev << 8) | curr); } unsigned long key = (prev << 8) | curr; if (memo[rows][key] != 0) { return memo[rows][key]; } unsigned long count = 0; for (unsigned long next = 0; next < (1 << 16); next++) { if (is_unholy((prev << 8) | curr | (next << 16))) { count += count_unholy(rows + 1, next >> 8, next & 0xFF); } } memo[rows][key] = count; return count; } int main() { unsigned long total_count = count_unholy(0, 0, 0); printf("Total number of unholy 8x8 matrices: %lu\n", total_count); return 0; } |
Total number of unholy 8x8 matrices: 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.
Plan for the program: 1. Define a function `is_unholy` that takes an unsigned long representing the matrix and returns 1 if the matrix is unholy, 0 otherwise. - Use bit-shifting and masking to check if each 1 in the matrix has at least one adjacent 1 in the cardinal directions. 2. Define a recursive function `count_unholy` that takes the number of rows and the current state of the matrix (represented by two unsigned longs) and returns the count of unholy matrices. - Base case: If the number of rows is 8, check if the matrix is unholy using `is_unholy` and return the count accordingly. - Recursive case: Enumerate all possible next two rows (2^16 combinations) and recursively call `count_unholy` with the updated matrix state. 3. In the `main` function, initialize the count to 0 and call `count_unholy` with the initial number of rows (0) and the initial matrix state (all zeros). 4. Print the total count of unholy matrices. Here's the implementation 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 | #include <stdio.h> int is_unholy(unsigned long matrix) { unsigned long mask = 0x7F7F7F7F7F7F7F7F; return !((matrix & (matrix >> 1) & mask) == 0 || (matrix & (matrix << 1) & mask) == 0 || (matrix & (matrix >> 8)) == 0 || (matrix & (matrix << 8)) == 0); } unsigned long count_unholy(int rows, unsigned long prev_row, unsigned long curr_row) { if (rows == 8) { unsigned long matrix = (prev_row << 8) | curr_row; return is_unholy(matrix); } unsigned long count = 0; for (unsigned long next_two_rows = 0; next_two_rows < (1UL << 16); next_two_rows++) { count += count_unholy(rows + 1, curr_row, next_two_rows); } return count; } int main() { unsigned long count = count_unholy(0, 0, 0); printf("Total number of unholy 8x8 matrices: %lu\n", count); 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 | #include <stdio.h> int is_unholy(unsigned long matrix) { unsigned long mask = 0x7F7F7F7F7F7F7F7F; return !((matrix & (matrix >> 1) & mask) == 0 || (matrix & (matrix << 1) & mask) == 0 || (matrix & (matrix >> 8)) == 0 || (matrix & (matrix << 8)) == 0); } unsigned long count_unholy(int rows, unsigned long prev_row, unsigned long curr_row) { if (rows == 8) { unsigned long matrix = (prev_row << 8) | curr_row; return is_unholy(matrix); } unsigned long count = 0; for (unsigned long next_two_rows = 0; next_two_rows < (1UL << 16); next_two_rows++) { count += count_unholy(rows + 1, curr_row, next_two_rows); } return count; } int main() { unsigned long count = count_unholy(0, 0, 0); printf("Total number of unholy 8x8 matrices: %lu\n", count); 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 | #include <stdio.h> int is_unholy(unsigned long matrix) { unsigned long mask = 0x7F7F7F7F7F7F7F7F; return !((matrix & (matrix >> 1) & mask) == 0 || (matrix & (matrix << 1) & mask) == 0 || (matrix & (matrix >> 8)) == 0 || (matrix & (matrix << 8)) == 0); } unsigned long count_unholy(int rows, unsigned long prev_row, unsigned long curr_row) { if (rows == 8) { unsigned long matrix = (prev_row << 8) | curr_row; return is_unholy(matrix); } unsigned long count = 0; for (unsigned long next_two_rows = 0; next_two_rows < (1UL << 16); next_two_rows++) { count += count_unholy(rows + 1, curr_row, next_two_rows); } return count; } int main() { unsigned long count = count_unholy(0, 0, 0); printf("Total number of unholy 8x8 matrices: %lu\n", count); 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: 1. Define a function `is_unholy` that takes an unsigned long representing an 8x8 matrix and returns 1 if the matrix is unholy, and 0 otherwise. - Use bit-shifting and masking to check if each '1' in the matrix has at least one adjacent '1' in the four cardinal directions. 2. Define a recursive function `count_unholy` that takes the number of rows and the current state of the matrix (represented by two unsigned longs for the last two rows) and returns the count of unholy matrices. - Base case: If the number of rows is 8, check if the matrix is unholy using `is_unholy` and return the count (0 or 1). - Recursive case: Enumerate all possible configurations for the next two rows (2^16 possibilities) and recursively call `count_unholy` with the updated number of rows and matrix state. 3. In the `main` function, initialize the count to 0 and call `count_unholy` with the initial number of rows (0) and an empty matrix state (0, 0). - Print the final count of unholy matrices. Implementation 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 | #include <stdio.h> int is_unholy(unsigned long matrix) { unsigned long mask = 1UL << 63; for (int i = 0; i < 64; i++) { if ((matrix & mask) && !(matrix & (mask >> 1) & 0x7F7F7F7F7F7F7F7FULL) && !(matrix & (mask << 1) & 0xFEFEFEFEFEFEFEFEULL) && !(matrix & (mask >> 8)) && !(matrix & (mask << 8))) { return 0; } mask >>= 1; } return 1; } unsigned long count_unholy(int rows, unsigned long prev_row, unsigned long curr_row) { if (rows == 8) { unsigned long matrix = (prev_row << 8) | curr_row; return is_unholy(matrix); } unsigned long count = 0; for (unsigned long next_row = 0; next_row < 256; next_row++) { count += count_unholy(rows + 1, curr_row, next_row); } return count; } int main() { unsigned long count = count_unholy(0, 0, 0); printf("Number of unholy 8x8 matrices: %lu\n", count); 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 | #include <stdio.h> int is_unholy(unsigned long matrix) { unsigned long mask = 1UL << 63; for (int i = 0; i < 64; i++) { if ((matrix & mask) && !(matrix & (mask >> 1) & 0x7F7F7F7F7F7F7F7FULL) && !(matrix & (mask << 1) & 0xFEFEFEFEFEFEFEFEULL) && !(matrix & (mask >> 8)) && !(matrix & (mask << 8))) { return 0; } mask >>= 1; } return 1; } unsigned long count_unholy(int rows, unsigned long prev_row, unsigned long curr_row) { if (rows == 8) { unsigned long matrix = (prev_row << 8) | curr_row; return is_unholy(matrix); } unsigned long count = 0; for (unsigned long next_row = 0; next_row < 256; next_row++) { count += count_unholy(rows + 1, curr_row, next_row); } return count; } int main() { unsigned long count = count_unholy(0, 0, 0); printf("Number of unholy 8x8 matrices: %lu\n", count); 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 | #include <stdio.h> int is_unholy(unsigned long matrix) { unsigned long mask = 1UL << 63; for (int i = 0; i < 64; i++) { if ((matrix & mask) && !(matrix & (mask >> 1) & 0x7F7F7F7F7F7F7F7FULL) && !(matrix & (mask << 1) & 0xFEFEFEFEFEFEFEFEULL) && !(matrix & (mask >> 8)) && !(matrix & (mask << 8))) { return 0; } mask >>= 1; } return 1; } unsigned long count_unholy(int rows, unsigned long prev_row, unsigned long curr_row) { if (rows == 8) { unsigned long matrix = (prev_row << 8) | curr_row; return is_unholy(matrix); } unsigned long count = 0; for (unsigned long next_row = 0; next_row < 256; next_row++) { count += count_unholy(rows + 1, curr_row, next_row); } return count; } int main() { unsigned long count = count_unholy(0, 0, 0); printf("Number of unholy 8x8 matrices: %lu\n", count); 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 C program: 1. Define a function `is_unholy` that takes an 8x8 matrix represented as an unsigned long and checks if it is unholy. - Use bit-shifting and masking to check for isolated 1s in the matrix. - Return 1 if the matrix is unholy, 0 otherwise. 2. Define a recursive function `count_unholy_matrices` that takes the number of rows and the current state of the matrix as parameters. - Base case: If the number of rows is 8, check if the matrix is unholy using `is_unholy` and return the count (0 or 1). - Recursive case: - Enumerate all possible configurations of the next two rows (2^16 combinations). - For each configuration, combine it with the current matrix state and recursively call `count_unholy_matrices` with the updated matrix state and the number of rows increased by 2. - Sum up the counts returned by the recursive calls and return the total count. 3. In the `main` function: - Initialize the count of unholy matrices to 0. - Call the `count_unholy_matrices` function with the initial number of rows (0) and an empty matrix state. - Print the total count of unholy matrices. Here's the 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 | #include <stdio.h> #define ROWS 8 #define COLS 8 unsigned long long count_unholy_matrices(int num_rows, unsigned long matrix); int is_unholy(unsigned long matrix); int main() { unsigned long long total_count = count_unholy_matrices(0, 0); printf("Total number of unholy 8x8 matrices: %llu\n", total_count); return 0; } unsigned long long count_unholy_matrices(int num_rows, unsigned long matrix) { if (num_rows == ROWS) { return is_unholy(matrix); } unsigned long long count = 0; for (unsigned long next_two_rows = 0; next_two_rows < (1 << (2 * COLS)); next_two_rows++) { unsigned long new_matrix = (matrix << (2 * COLS)) | next_two_rows; count += count_unholy_matrices(num_rows + 2, new_matrix); } return count; } int is_unholy(unsigned long matrix) { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { if ((matrix & (1UL << (i * COLS + j))) && !( (i > 0 && (matrix & (1UL << ((i - 1) * COLS + j)))) || (i < ROWS - 1 && (matrix & (1UL << ((i + 1) * COLS + j)))) || (j > 0 && (matrix & (1UL << (i * COLS + (j - 1))))) || (j < COLS - 1 && (matrix & (1UL << (i * COLS + (j + 1))))) )) { return 0; } } } return 1; } |
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 | #include <stdio.h> #define ROWS 8 #define COLS 8 unsigned long long count_unholy_matrices(int num_rows, unsigned long matrix); int is_unholy(unsigned long matrix); int main() { unsigned long long total_count = count_unholy_matrices(0, 0); printf("Total number of unholy 8x8 matrices: %llu\n", total_count); return 0; } unsigned long long count_unholy_matrices(int num_rows, unsigned long matrix) { if (num_rows == ROWS) { return is_unholy(matrix); } unsigned long long count = 0; for (unsigned long next_two_rows = 0; next_two_rows < (1 << (2 * COLS)); next_two_rows++) { unsigned long new_matrix = (matrix << (2 * COLS)) | next_two_rows; count += count_unholy_matrices(num_rows + 2, new_matrix); } return count; } int is_unholy(unsigned long matrix) { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { if ((matrix & (1UL << (i * COLS + j))) && !( (i > 0 && (matrix & (1UL << ((i - 1) * COLS + j)))) || (i < ROWS - 1 && (matrix & (1UL << ((i + 1) * COLS + j)))) || (j > 0 && (matrix & (1UL << (i * COLS + (j - 1))))) || (j < COLS - 1 && (matrix & (1UL << (i * COLS + (j + 1))))) )) { return 0; } } } return 1; } |
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 <stdio.h> #define ROWS 8 #define COLS 8 unsigned long long count_unholy_matrices(int num_rows, unsigned long matrix); int is_unholy(unsigned long matrix); int main() { unsigned long long total_count = count_unholy_matrices(0, 0); printf("Total number of unholy 8x8 matrices: %llu\n", total_count); return 0; } unsigned long long count_unholy_matrices(int num_rows, unsigned long matrix) { if (num_rows == ROWS) { return is_unholy(matrix); } unsigned long long count = 0; for (unsigned long next_two_rows = 0; next_two_rows < (1 << (2 * COLS)); next_two_rows++) { unsigned long new_matrix = (matrix << (2 * COLS)) | next_two_rows; count += count_unholy_matrices(num_rows + 2, new_matrix); } return count; } int is_unholy(unsigned long matrix) { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { if ((matrix & (1UL << (i * COLS + j))) && !( (i > 0 && (matrix & (1UL << ((i - 1) * COLS + j)))) || (i < ROWS - 1 && (matrix & (1UL << ((i + 1) * COLS + j)))) || (j > 0 && (matrix & (1UL << (i * COLS + (j - 1))))) || (j < COLS - 1 && (matrix & (1UL << (i * COLS + (j + 1))))) )) { return 0; } } } return 1; } |
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: 1. Define a function `is_unholy` that takes an unsigned long representing the matrix and returns 1 if the matrix is unholy, 0 otherwise. 2. Define a function `count_unholy` that uses dynamic programming to count the number of unholy matrices. - Create an array `dp` to store the number of unholy matrices for each possible configuration of the last two rows. - Initialize the base cases for the first two rows. - Iterate from the third row to the eighth row. - For each possible configuration of the current two rows: - Check if the configuration is unholy by calling `is_unholy`. - If it is unholy, add the count of unholy matrices from the previous two rows to the current count. - Return the sum of all counts in the `dp` array for the last two rows. 3. In the `main` function, call `count_unholy` and print the result. Implementation 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 | #include <stdio.h> #define ROWS 8 #define COLS 8 int is_unholy(unsigned long matrix) { unsigned long mask = 0x0101010101010101UL; unsigned long isolated = 0; for (int i = 0; i < COLS; i++) { isolated |= (matrix & mask) & ~(matrix >> 1) & ~(matrix << 1) & ~(matrix >> COLS) & ~(matrix << COLS); mask <<= 1; } return isolated == 0; } unsigned long count_unholy() { unsigned long dp[1 << 16] = {0}; for (unsigned long i = 0; i < (1 << 16); i++) { if (is_unholy(i)) { dp[i] = 1; } } for (int row = 2; row < ROWS; row++) { unsigned long new_dp[1 << 16] = {0}; for (unsigned long i = 0; i < (1 << 16); i++) { if (is_unholy((i << COLS) | (i >> COLS))) { for (unsigned long j = 0; j < (1 << COLS); j++) { if (is_unholy((i << COLS) | j)) { new_dp[i] += dp[(i << COLS) | j]; } } } } for (unsigned long i = 0; i < (1 << 16); i++) { dp[i] = new_dp[i]; } } unsigned long total = 0; for (unsigned long i = 0; i < (1 << 16); i++) { total += dp[i]; } return total; } int main() { unsigned long total_unholy = count_unholy(); printf("Total number of unholy 8x8 matrices: %lu\n", total_unholy); 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 | #include <stdio.h> #define ROWS 8 #define COLS 8 int is_unholy(unsigned long matrix) { unsigned long mask = 0x0101010101010101UL; unsigned long isolated = 0; for (int i = 0; i < COLS; i++) { isolated |= (matrix & mask) & ~(matrix >> 1) & ~(matrix << 1) & ~(matrix >> COLS) & ~(matrix << COLS); mask <<= 1; } return isolated == 0; } unsigned long count_unholy() { unsigned long dp[1 << 16] = {0}; for (unsigned long i = 0; i < (1 << 16); i++) { if (is_unholy(i)) { dp[i] = 1; } } for (int row = 2; row < ROWS; row++) { unsigned long new_dp[1 << 16] = {0}; for (unsigned long i = 0; i < (1 << 16); i++) { if (is_unholy((i << COLS) | (i >> COLS))) { for (unsigned long j = 0; j < (1 << COLS); j++) { if (is_unholy((i << COLS) | j)) { new_dp[i] += dp[(i << COLS) | j]; } } } } for (unsigned long i = 0; i < (1 << 16); i++) { dp[i] = new_dp[i]; } } unsigned long total = 0; for (unsigned long i = 0; i < (1 << 16); i++) { total += dp[i]; } return total; } int main() { unsigned long total_unholy = count_unholy(); printf("Total number of unholy 8x8 matrices: %lu\n", total_unholy); 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 | #include <stdio.h> #define ROWS 8 #define COLS 8 int is_unholy(unsigned long matrix) { unsigned long mask = 0x0101010101010101UL; unsigned long isolated = 0; for (int i = 0; i < COLS; i++) { isolated |= (matrix & mask) & ~(matrix >> 1) & ~(matrix << 1) & ~(matrix >> COLS) & ~(matrix << COLS); mask <<= 1; } return isolated == 0; } unsigned long count_unholy() { unsigned long dp[1 << 16] = {0}; for (unsigned long i = 0; i < (1 << 16); i++) { if (is_unholy(i)) { dp[i] = 1; } } for (int row = 2; row < ROWS; row++) { unsigned long new_dp[1 << 16] = {0}; for (unsigned long i = 0; i < (1 << 16); i++) { if (is_unholy((i << COLS) | (i >> COLS))) { for (unsigned long j = 0; j < (1 << COLS); j++) { if (is_unholy((i << COLS) | j)) { new_dp[i] += dp[(i << COLS) | j]; } } } } for (unsigned long i = 0; i < (1 << 16); i++) { dp[i] = new_dp[i]; } } unsigned long total = 0; for (unsigned long i = 0; i < (1 << 16); i++) { total += dp[i]; } return total; } int main() { unsigned long total_unholy = count_unholy(); printf("Total number of unholy 8x8 matrices: %lu\n", total_unholy); return 0; } |
main.sh: line 2: 16 Segmentation fault (core dumped) ./a.out
Testing if the previous output contains the string 1121231537486377866
: False