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 comprehensive solution to the problem: Program Plan: 1. Data Structures: - Use unsigned long (64 bits) to represent each row - Use dynamic programming with memoization - Create a memo to store intermediate results 2. Key Functions: a. is_isolated(row, above_row): - Check if any 1 in current row is isolated - Use bit manipulation to check cardinal directions b. is_unholy(matrix, num_rows): - Iterate through matrix - Use is_isolated to check each cell - Return false if any isolated 1 found c. count_unholy_matrices(): - Use recursive DP - Memoize results based on current rows - Explore all possible next two rows - Prune impossible configurations Here's the implementation:
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 80 81 | #include <stdio.h> #include <stdint.h> #include <string.h> #define MATRIX_SIZE 8 #define MEMO_SIZE (1 << 16) uint64_t memo[MATRIX_SIZE + 1][MEMO_SIZE]; int memo_initialized = 0; // Check if a 1 is isolated in a specific position int is_isolated(uint64_t row, uint64_t above_row, int col) { // Check if current cell is 1 if (!(row & (1ULL << col))) return 0; // Check cardinal directions int left = (col > 0) && (row & (1ULL << (col-1))); int right = (col < 7) && (row & (1ULL << (col+1))); int up = (above_row & (1ULL << col)); return !(left || right || up); } // Check if matrix configuration is unholy int is_unholy(uint64_t prev_row, uint64_t current_row) { for (int col = 0; col < MATRIX_SIZE; col++) { if (is_isolated(current_row, prev_row, col)) { return 0; // Found an isolated 1 } } return 1; // No isolated 1s } // Dynamic programming to count unholy matrices uint64_t count_unholy_matrices_dp(int rows_left, uint64_t prev_rows) { // Base case: completed matrix if (rows_left == 0) return 1; // Check memoization if (memo_initialized && memo[rows_left][prev_rows] != 0) { return memo[rows_left][prev_rows]; } uint64_t total_count = 0; // Try all possible next two rows for (uint64_t row1 = 0; row1 < (1ULL << MATRIX_SIZE); row1++) { for (uint64_t row2 = 0; row2 < (1ULL << MATRIX_SIZE); row2++) { // Check if configuration is valid if (is_unholy(prev_rows, row1) && is_unholy(row1, row2)) { // Recursive call total_count += count_unholy_matrices_dp( rows_left - 2, row2 ); } } } // Memoize result if (memo_initialized) { memo[rows_left][prev_rows] = total_count; } return total_count; } uint64_t count_unholy_matrices() { memo_initialized = 1; memset(memo, 0, sizeof(memo)); return count_unholy_matrices_dp(MATRIX_SIZE, 0); } int main() { uint64_t result = count_unholy_matrices(); printf("Total number of unholy 8x8 matrices: %llu\n", result); return 0; } |
1 2 | gcc -O3 unholy_matrices.c -o unholy_matrices ./unholy_matrices |
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 80 81 | #include <stdio.h> #include <stdint.h> #include <string.h> #define MATRIX_SIZE 8 #define MEMO_SIZE (1 << 16) uint64_t memo[MATRIX_SIZE + 1][MEMO_SIZE]; int memo_initialized = 0; // Check if a 1 is isolated in a specific position int is_isolated(uint64_t row, uint64_t above_row, int col) { // Check if current cell is 1 if (!(row & (1ULL << col))) return 0; // Check cardinal directions int left = (col > 0) && (row & (1ULL << (col-1))); int right = (col < 7) && (row & (1ULL << (col+1))); int up = (above_row & (1ULL << col)); return !(left || right || up); } // Check if matrix configuration is unholy int is_unholy(uint64_t prev_row, uint64_t current_row) { for (int col = 0; col < MATRIX_SIZE; col++) { if (is_isolated(current_row, prev_row, col)) { return 0; // Found an isolated 1 } } return 1; // No isolated 1s } // Dynamic programming to count unholy matrices uint64_t count_unholy_matrices_dp(int rows_left, uint64_t prev_rows) { // Base case: completed matrix if (rows_left == 0) return 1; // Check memoization if (memo_initialized && memo[rows_left][prev_rows] != 0) { return memo[rows_left][prev_rows]; } uint64_t total_count = 0; // Try all possible next two rows for (uint64_t row1 = 0; row1 < (1ULL << MATRIX_SIZE); row1++) { for (uint64_t row2 = 0; row2 < (1ULL << MATRIX_SIZE); row2++) { // Check if configuration is valid if (is_unholy(prev_rows, row1) && is_unholy(row1, row2)) { // Recursive call total_count += count_unholy_matrices_dp( rows_left - 2, row2 ); } } } // Memoize result if (memo_initialized) { memo[rows_left][prev_rows] = total_count; } return total_count; } uint64_t count_unholy_matrices() { memo_initialized = 1; memset(memo, 0, sizeof(memo)); return count_unholy_matrices_dp(MATRIX_SIZE, 0); } int main() { uint64_t result = count_unholy_matrices(); printf("Total number of unholy 8x8 matrices: %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 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 80 81 | #include <stdio.h> #include <stdint.h> #include <string.h> #define MATRIX_SIZE 8 #define MEMO_SIZE (1 << 16) uint64_t memo[MATRIX_SIZE + 1][MEMO_SIZE]; int memo_initialized = 0; // Check if a 1 is isolated in a specific position int is_isolated(uint64_t row, uint64_t above_row, int col) { // Check if current cell is 1 if (!(row & (1ULL << col))) return 0; // Check cardinal directions int left = (col > 0) && (row & (1ULL << (col-1))); int right = (col < 7) && (row & (1ULL << (col+1))); int up = (above_row & (1ULL << col)); return !(left || right || up); } // Check if matrix configuration is unholy int is_unholy(uint64_t prev_row, uint64_t current_row) { for (int col = 0; col < MATRIX_SIZE; col++) { if (is_isolated(current_row, prev_row, col)) { return 0; // Found an isolated 1 } } return 1; // No isolated 1s } // Dynamic programming to count unholy matrices uint64_t count_unholy_matrices_dp(int rows_left, uint64_t prev_rows) { // Base case: completed matrix if (rows_left == 0) return 1; // Check memoization if (memo_initialized && memo[rows_left][prev_rows] != 0) { return memo[rows_left][prev_rows]; } uint64_t total_count = 0; // Try all possible next two rows for (uint64_t row1 = 0; row1 < (1ULL << MATRIX_SIZE); row1++) { for (uint64_t row2 = 0; row2 < (1ULL << MATRIX_SIZE); row2++) { // Check if configuration is valid if (is_unholy(prev_rows, row1) && is_unholy(row1, row2)) { // Recursive call total_count += count_unholy_matrices_dp( rows_left - 2, row2 ); } } } // Memoize result if (memo_initialized) { memo[rows_left][prev_rows] = total_count; } return total_count; } uint64_t count_unholy_matrices() { memo_initialized = 1; memset(memo, 0, sizeof(memo)); return count_unholy_matrices_dp(MATRIX_SIZE, 0); } int main() { uint64_t result = count_unholy_matrices(); printf("Total number of unholy 8x8 matrices: %llu\n", result); return 0; } |
Total number of unholy 8x8 matrices: 53003772538202116
main.c: In function 'main':
main.c:80:53: warning: format '%llu' expects argument of type 'long long unsigned int', but argument 2 has type 'uint64_t' {aka 'long unsigned int'} [-Wformat=]
80 | printf("Total number of unholy 8x8 matrices: %llu\n", result);
| ~~~^ ~~~~~~
| | |
| | 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 comprehensive plan and implementation for solving the unholy matrix problem: Plan: 1. Define key data structures and constants 2. Implement bit manipulation helper functions 3. Create an is_unholy function 4. Develop a dynamic programming solution with memoization 5. Main function to drive the computation Here's an implementation following this plan:
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 80 81 82 83 84 85 86 87 88 89 90 | #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> #define MATRIX_SIZE 8 #define MAX_ROWS (1 << MATRIX_SIZE) // Memoization cache uint64_t memo[MATRIX_SIZE + 1][MAX_ROWS][MAX_ROWS]; int memo_initialized = 0; // Function to check if a specific cell is isolated int is_isolated(uint64_t matrix, int row, int col) { // Check if the bit is set if (!(matrix & (1ULL << (row * MATRIX_SIZE + col)))) { return 0; } // Cardinal direction masks uint64_t up_mask = (row > 0) ? (1ULL << ((row-1) * MATRIX_SIZE + col)) : 0; uint64_t down_mask = (row < MATRIX_SIZE-1) ? (1ULL << ((row+1) * MATRIX_SIZE + col)) : 0; uint64_t left_mask = (col > 0) ? (1ULL << (row * MATRIX_SIZE + col - 1)) : 0; uint64_t right_mask = (col < MATRIX_SIZE-1) ? (1ULL << (row * MATRIX_SIZE + col + 1)) : 0; // Check if no neighboring cells have 1s return !(matrix & (up_mask | down_mask | left_mask | right_mask)); } // Check if entire matrix is unholy int is_unholy(uint64_t matrix) { for (int row = 0; row < MATRIX_SIZE; row++) { for (int col = 0; col < MATRIX_SIZE; col++) { if (is_isolated(matrix, row, col)) { return 0; // Found an isolated 1 } } } return 1; // No isolated 1s } // Dynamic programming solution uint64_t count_unholy_matrices(int current_row, uint64_t prev_rows, uint64_t current_rows) { // Base case: completed matrix if (current_row == MATRIX_SIZE) { return 1; } // Check memoization cache if (memo_initialized && memo[current_row][prev_rows][current_rows] != 0) { return memo[current_row][prev_rows][current_rows]; } uint64_t total_count = 0; // Try all possible configurations for the next two rows for (int next_row_config = 0; next_row_config < (1 << (2 * MATRIX_SIZE)); next_row_config++) { uint64_t combined_matrix = prev_rows | (current_rows << MATRIX_SIZE) | ((uint64_t)next_row_config << (2 * MATRIX_SIZE)); // Check if current configuration is valid if (is_unholy(combined_matrix)) { total_count += count_unholy_matrices( current_row + 1, current_rows, next_row_config & ((1 << MATRIX_SIZE) - 1) ); } } // Memoize result if (memo_initialized) { memo[current_row][prev_rows][current_rows] = total_count; } return total_count; } int main() { // Initialize memoization cache memo_initialized = 1; memset(memo, 0, sizeof(memo)); // Compute total unholy matrices uint64_t total = count_unholy_matrices(0, 0, 0); printf("Total number of unholy 8x8 matrices: %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 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 80 81 82 83 84 85 86 87 88 89 90 | #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> #define MATRIX_SIZE 8 #define MAX_ROWS (1 << MATRIX_SIZE) // Memoization cache uint64_t memo[MATRIX_SIZE + 1][MAX_ROWS][MAX_ROWS]; int memo_initialized = 0; // Function to check if a specific cell is isolated int is_isolated(uint64_t matrix, int row, int col) { // Check if the bit is set if (!(matrix & (1ULL << (row * MATRIX_SIZE + col)))) { return 0; } // Cardinal direction masks uint64_t up_mask = (row > 0) ? (1ULL << ((row-1) * MATRIX_SIZE + col)) : 0; uint64_t down_mask = (row < MATRIX_SIZE-1) ? (1ULL << ((row+1) * MATRIX_SIZE + col)) : 0; uint64_t left_mask = (col > 0) ? (1ULL << (row * MATRIX_SIZE + col - 1)) : 0; uint64_t right_mask = (col < MATRIX_SIZE-1) ? (1ULL << (row * MATRIX_SIZE + col + 1)) : 0; // Check if no neighboring cells have 1s return !(matrix & (up_mask | down_mask | left_mask | right_mask)); } // Check if entire matrix is unholy int is_unholy(uint64_t matrix) { for (int row = 0; row < MATRIX_SIZE; row++) { for (int col = 0; col < MATRIX_SIZE; col++) { if (is_isolated(matrix, row, col)) { return 0; // Found an isolated 1 } } } return 1; // No isolated 1s } // Dynamic programming solution uint64_t count_unholy_matrices(int current_row, uint64_t prev_rows, uint64_t current_rows) { // Base case: completed matrix if (current_row == MATRIX_SIZE) { return 1; } // Check memoization cache if (memo_initialized && memo[current_row][prev_rows][current_rows] != 0) { return memo[current_row][prev_rows][current_rows]; } uint64_t total_count = 0; // Try all possible configurations for the next two rows for (int next_row_config = 0; next_row_config < (1 << (2 * MATRIX_SIZE)); next_row_config++) { uint64_t combined_matrix = prev_rows | (current_rows << MATRIX_SIZE) | ((uint64_t)next_row_config << (2 * MATRIX_SIZE)); // Check if current configuration is valid if (is_unholy(combined_matrix)) { total_count += count_unholy_matrices( current_row + 1, current_rows, next_row_config & ((1 << MATRIX_SIZE) - 1) ); } } // Memoize result if (memo_initialized) { memo[current_row][prev_rows][current_rows] = total_count; } return total_count; } int main() { // Initialize memoization cache memo_initialized = 1; memset(memo, 0, sizeof(memo)); // Compute total unholy matrices uint64_t total = count_unholy_matrices(0, 0, 0); printf("Total number of unholy 8x8 matrices: %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 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 80 81 82 83 84 85 86 87 88 89 90 | #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> #define MATRIX_SIZE 8 #define MAX_ROWS (1 << MATRIX_SIZE) // Memoization cache uint64_t memo[MATRIX_SIZE + 1][MAX_ROWS][MAX_ROWS]; int memo_initialized = 0; // Function to check if a specific cell is isolated int is_isolated(uint64_t matrix, int row, int col) { // Check if the bit is set if (!(matrix & (1ULL << (row * MATRIX_SIZE + col)))) { return 0; } // Cardinal direction masks uint64_t up_mask = (row > 0) ? (1ULL << ((row-1) * MATRIX_SIZE + col)) : 0; uint64_t down_mask = (row < MATRIX_SIZE-1) ? (1ULL << ((row+1) * MATRIX_SIZE + col)) : 0; uint64_t left_mask = (col > 0) ? (1ULL << (row * MATRIX_SIZE + col - 1)) : 0; uint64_t right_mask = (col < MATRIX_SIZE-1) ? (1ULL << (row * MATRIX_SIZE + col + 1)) : 0; // Check if no neighboring cells have 1s return !(matrix & (up_mask | down_mask | left_mask | right_mask)); } // Check if entire matrix is unholy int is_unholy(uint64_t matrix) { for (int row = 0; row < MATRIX_SIZE; row++) { for (int col = 0; col < MATRIX_SIZE; col++) { if (is_isolated(matrix, row, col)) { return 0; // Found an isolated 1 } } } return 1; // No isolated 1s } // Dynamic programming solution uint64_t count_unholy_matrices(int current_row, uint64_t prev_rows, uint64_t current_rows) { // Base case: completed matrix if (current_row == MATRIX_SIZE) { return 1; } // Check memoization cache if (memo_initialized && memo[current_row][prev_rows][current_rows] != 0) { return memo[current_row][prev_rows][current_rows]; } uint64_t total_count = 0; // Try all possible configurations for the next two rows for (int next_row_config = 0; next_row_config < (1 << (2 * MATRIX_SIZE)); next_row_config++) { uint64_t combined_matrix = prev_rows | (current_rows << MATRIX_SIZE) | ((uint64_t)next_row_config << (2 * MATRIX_SIZE)); // Check if current configuration is valid if (is_unholy(combined_matrix)) { total_count += count_unholy_matrices( current_row + 1, current_rows, next_row_config & ((1 << MATRIX_SIZE) - 1) ); } } // Memoize result if (memo_initialized) { memo[current_row][prev_rows][current_rows] = total_count; } return total_count; } int main() { // Initialize memoization cache memo_initialized = 1; memset(memo, 0, sizeof(memo)); // Compute total unholy matrices uint64_t total = count_unholy_matrices(0, 0, 0); printf("Total number of unholy 8x8 matrices: %llu\n", total); 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.
Here's a comprehensive plan and implementation for solving the unholy matrix problem: Program Plan: 1. Data Structures - Use unsigned long (64-bit) to represent each row - Use dynamic programming with memoization - Create a cache to store intermediate results 2. Core Functions: a) is_unholy function - Check if a 1 is isolated by checking cardinal directions - Use bit manipulation to test adjacency b) count_unholy_matrices function - Recursive with memoization - Base case: when all 8 rows are filled - Recursive case: try all possible next two rows - Memoize results to avoid recomputing 3. Main function - Initialize memoization cache - Call recursive function - Print total count of unholy matrices Here's a C implementation following this plan:
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 80 81 82 83 | #include <stdio.h> #include <stdint.h> #include <string.h> #define SIZE 8 #define CACHE_SIZE (1ULL << 16) // Memoization cache uint64_t cache[SIZE + 1][CACHE_SIZE]; int cache_initialized = 0; // Check if a 1 is isolated in a row int is_isolated(unsigned long row, int pos) { // Check left and right int left_check = (pos > 0 && (row & (1UL << (pos-1)))); int right_check = (pos < SIZE-1 && (row & (1UL << (pos+1)))); return !(left_check || right_check); } // Check if a matrix configuration is unholy int is_unholy(unsigned long prev_row, unsigned long curr_row) { for (int i = 0; i < SIZE; i++) { if (curr_row & (1UL << i)) { // Check if 1 is isolated vertically and horizontally if (is_isolated(curr_row, i) && (!prev_row || is_isolated(prev_row, i))) { return 0; // Found an isolated 1 } } } return 1; // No isolated 1s } // Recursive function with memoization to count unholy matrices uint64_t count_unholy_matrices(int row, unsigned long prev_rows) { // Base case: all rows filled if (row == SIZE) return 1; // Check memoized result if (cache_initialized && cache[row][prev_rows] != 0) { return cache[row][prev_rows]; } uint64_t total = 0; // Try all possible 2^16 configurations for next two rows for (unsigned long next_row1 = 0; next_row1 < (1UL << SIZE); next_row1++) { for (unsigned long next_row2 = 0; next_row2 < (1UL << SIZE); next_row2++) { // Extract previous rows from state unsigned long prev_row1 = (prev_rows >> SIZE) & ((1UL << SIZE) - 1); unsigned long prev_row2 = prev_rows & ((1UL << SIZE) - 1); // Check if current configuration is unholy if (is_unholy(prev_row1, next_row1) && is_unholy(next_row1, next_row2)) { // Recursive call with updated state unsigned long new_state = (next_row1 << SIZE) | next_row2; total += count_unholy_matrices(row + 2, new_state); } } } // Memoize result if (cache_initialized) { cache[row][prev_rows] = total; } return total; } int main() { // Initialize cache cache_initialized = 1; memset(cache, 0, sizeof(cache)); // Count unholy matrices uint64_t result = count_unholy_matrices(0, 0); printf("Total number of unholy 8x8 matrices: %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 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 80 81 82 83 | #include <stdio.h> #include <stdint.h> #include <string.h> #define SIZE 8 #define CACHE_SIZE (1ULL << 16) // Memoization cache uint64_t cache[SIZE + 1][CACHE_SIZE]; int cache_initialized = 0; // Check if a 1 is isolated in a row int is_isolated(unsigned long row, int pos) { // Check left and right int left_check = (pos > 0 && (row & (1UL << (pos-1)))); int right_check = (pos < SIZE-1 && (row & (1UL << (pos+1)))); return !(left_check || right_check); } // Check if a matrix configuration is unholy int is_unholy(unsigned long prev_row, unsigned long curr_row) { for (int i = 0; i < SIZE; i++) { if (curr_row & (1UL << i)) { // Check if 1 is isolated vertically and horizontally if (is_isolated(curr_row, i) && (!prev_row || is_isolated(prev_row, i))) { return 0; // Found an isolated 1 } } } return 1; // No isolated 1s } // Recursive function with memoization to count unholy matrices uint64_t count_unholy_matrices(int row, unsigned long prev_rows) { // Base case: all rows filled if (row == SIZE) return 1; // Check memoized result if (cache_initialized && cache[row][prev_rows] != 0) { return cache[row][prev_rows]; } uint64_t total = 0; // Try all possible 2^16 configurations for next two rows for (unsigned long next_row1 = 0; next_row1 < (1UL << SIZE); next_row1++) { for (unsigned long next_row2 = 0; next_row2 < (1UL << SIZE); next_row2++) { // Extract previous rows from state unsigned long prev_row1 = (prev_rows >> SIZE) & ((1UL << SIZE) - 1); unsigned long prev_row2 = prev_rows & ((1UL << SIZE) - 1); // Check if current configuration is unholy if (is_unholy(prev_row1, next_row1) && is_unholy(next_row1, next_row2)) { // Recursive call with updated state unsigned long new_state = (next_row1 << SIZE) | next_row2; total += count_unholy_matrices(row + 2, new_state); } } } // Memoize result if (cache_initialized) { cache[row][prev_rows] = total; } return total; } int main() { // Initialize cache cache_initialized = 1; memset(cache, 0, sizeof(cache)); // Count unholy matrices uint64_t result = count_unholy_matrices(0, 0); printf("Total number of unholy 8x8 matrices: %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 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 80 81 82 83 | #include <stdio.h> #include <stdint.h> #include <string.h> #define SIZE 8 #define CACHE_SIZE (1ULL << 16) // Memoization cache uint64_t cache[SIZE + 1][CACHE_SIZE]; int cache_initialized = 0; // Check if a 1 is isolated in a row int is_isolated(unsigned long row, int pos) { // Check left and right int left_check = (pos > 0 && (row & (1UL << (pos-1)))); int right_check = (pos < SIZE-1 && (row & (1UL << (pos+1)))); return !(left_check || right_check); } // Check if a matrix configuration is unholy int is_unholy(unsigned long prev_row, unsigned long curr_row) { for (int i = 0; i < SIZE; i++) { if (curr_row & (1UL << i)) { // Check if 1 is isolated vertically and horizontally if (is_isolated(curr_row, i) && (!prev_row || is_isolated(prev_row, i))) { return 0; // Found an isolated 1 } } } return 1; // No isolated 1s } // Recursive function with memoization to count unholy matrices uint64_t count_unholy_matrices(int row, unsigned long prev_rows) { // Base case: all rows filled if (row == SIZE) return 1; // Check memoized result if (cache_initialized && cache[row][prev_rows] != 0) { return cache[row][prev_rows]; } uint64_t total = 0; // Try all possible 2^16 configurations for next two rows for (unsigned long next_row1 = 0; next_row1 < (1UL << SIZE); next_row1++) { for (unsigned long next_row2 = 0; next_row2 < (1UL << SIZE); next_row2++) { // Extract previous rows from state unsigned long prev_row1 = (prev_rows >> SIZE) & ((1UL << SIZE) - 1); unsigned long prev_row2 = prev_rows & ((1UL << SIZE) - 1); // Check if current configuration is unholy if (is_unholy(prev_row1, next_row1) && is_unholy(next_row1, next_row2)) { // Recursive call with updated state unsigned long new_state = (next_row1 << SIZE) | next_row2; total += count_unholy_matrices(row + 2, new_state); } } } // Memoize result if (cache_initialized) { cache[row][prev_rows] = total; } return total; } int main() { // Initialize cache cache_initialized = 1; memset(cache, 0, sizeof(cache)); // Count unholy matrices uint64_t result = count_unholy_matrices(0, 0); printf("Total number of unholy 8x8 matrices: %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.
Here's a comprehensive solution to the problem: Plan: 1. Define a matrix representation using unsigned long (64 bits) 2. Create an is_isolated function to check if a single cell is isolated 3. Create an is_unholy function that checks the entire matrix 4. Implement a dynamic programming solution with memoization 5. Main function to calculate and print the total number of unholy matrices Here's the implementation:
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 80 81 82 83 84 85 86 87 88 89 90 91 92 | #include <stdio.h> #include <stdint.h> #include <string.h> #define MATRIX_SIZE 8 #define MAX_MEMO_SIZE (1 << 16) // Check if a specific bit is 1 #define IS_BIT_SET(val, pos) ((val) & (1ULL << (pos))) // Memoization table uint64_t memo[MATRIX_SIZE + 1][MAX_MEMO_SIZE]; int memo_init[MATRIX_SIZE + 1][MAX_MEMO_SIZE]; // Check if a single cell is isolated int is_isolated(unsigned long matrix, int row, int col) { // Check if current cell is 0 if (!IS_BIT_SET(matrix, row * MATRIX_SIZE + col)) return 0; // Check left if (col > 0 && IS_BIT_SET(matrix, row * MATRIX_SIZE + col - 1)) return 0; // Check right if (col < MATRIX_SIZE - 1 && IS_BIT_SET(matrix, row * MATRIX_SIZE + col + 1)) return 0; // Check up if (row > 0 && IS_BIT_SET(matrix, (row - 1) * MATRIX_SIZE + col)) return 0; // Check down if (row < MATRIX_SIZE - 1 && IS_BIT_SET(matrix, (row + 1) * MATRIX_SIZE + col)) return 0; return 1; } // Check if the entire matrix is unholy int is_unholy(unsigned long matrix) { for (int row = 0; row < MATRIX_SIZE; row++) { for (int col = 0; col < MATRIX_SIZE; col++) { if (is_isolated(matrix, row, col)) return 0; } } return 1; } // Dynamic programming to count unholy matrices uint64_t count_unholy_matrices(int rows_left, unsigned long prev_rows) { // Base case: if we've filled all rows if (rows_left == 0) return 1; // Check memoization if (memo_init[rows_left][prev_rows]) return memo[rows_left][prev_rows]; uint64_t total_count = 0; // Try all possible next two rows for (unsigned long next_rows = 0; next_rows < (1ULL << (2 * MATRIX_SIZE)); next_rows++) { // Combine previous rows and new rows unsigned long current_matrix = (prev_rows << (2 * MATRIX_SIZE)) | next_rows; // Check if this configuration is unholy if (is_unholy(current_matrix)) { total_count += count_unholy_matrices(rows_left - 2, next_rows); } } // Memoize and return memo[rows_left][prev_rows] = total_count; memo_init[rows_left][prev_rows] = 1; return total_count; } int main() { // Clear memoization table memset(memo, 0, sizeof(memo)); memset(memo_init, 0, sizeof(memo_init)); // Start with 0 previous rows, 8 rows to fill uint64_t result = count_unholy_matrices(MATRIX_SIZE, 0); printf("Total number of unholy 8x8 matrices: %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 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 80 81 82 83 84 85 86 87 88 89 90 91 92 | #include <stdio.h> #include <stdint.h> #include <string.h> #define MATRIX_SIZE 8 #define MAX_MEMO_SIZE (1 << 16) // Check if a specific bit is 1 #define IS_BIT_SET(val, pos) ((val) & (1ULL << (pos))) // Memoization table uint64_t memo[MATRIX_SIZE + 1][MAX_MEMO_SIZE]; int memo_init[MATRIX_SIZE + 1][MAX_MEMO_SIZE]; // Check if a single cell is isolated int is_isolated(unsigned long matrix, int row, int col) { // Check if current cell is 0 if (!IS_BIT_SET(matrix, row * MATRIX_SIZE + col)) return 0; // Check left if (col > 0 && IS_BIT_SET(matrix, row * MATRIX_SIZE + col - 1)) return 0; // Check right if (col < MATRIX_SIZE - 1 && IS_BIT_SET(matrix, row * MATRIX_SIZE + col + 1)) return 0; // Check up if (row > 0 && IS_BIT_SET(matrix, (row - 1) * MATRIX_SIZE + col)) return 0; // Check down if (row < MATRIX_SIZE - 1 && IS_BIT_SET(matrix, (row + 1) * MATRIX_SIZE + col)) return 0; return 1; } // Check if the entire matrix is unholy int is_unholy(unsigned long matrix) { for (int row = 0; row < MATRIX_SIZE; row++) { for (int col = 0; col < MATRIX_SIZE; col++) { if (is_isolated(matrix, row, col)) return 0; } } return 1; } // Dynamic programming to count unholy matrices uint64_t count_unholy_matrices(int rows_left, unsigned long prev_rows) { // Base case: if we've filled all rows if (rows_left == 0) return 1; // Check memoization if (memo_init[rows_left][prev_rows]) return memo[rows_left][prev_rows]; uint64_t total_count = 0; // Try all possible next two rows for (unsigned long next_rows = 0; next_rows < (1ULL << (2 * MATRIX_SIZE)); next_rows++) { // Combine previous rows and new rows unsigned long current_matrix = (prev_rows << (2 * MATRIX_SIZE)) | next_rows; // Check if this configuration is unholy if (is_unholy(current_matrix)) { total_count += count_unholy_matrices(rows_left - 2, next_rows); } } // Memoize and return memo[rows_left][prev_rows] = total_count; memo_init[rows_left][prev_rows] = 1; return total_count; } int main() { // Clear memoization table memset(memo, 0, sizeof(memo)); memset(memo_init, 0, sizeof(memo_init)); // Start with 0 previous rows, 8 rows to fill uint64_t result = count_unholy_matrices(MATRIX_SIZE, 0); printf("Total number of unholy 8x8 matrices: %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 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 80 81 82 83 84 85 86 87 88 89 90 91 92 | #include <stdio.h> #include <stdint.h> #include <string.h> #define MATRIX_SIZE 8 #define MAX_MEMO_SIZE (1 << 16) // Check if a specific bit is 1 #define IS_BIT_SET(val, pos) ((val) & (1ULL << (pos))) // Memoization table uint64_t memo[MATRIX_SIZE + 1][MAX_MEMO_SIZE]; int memo_init[MATRIX_SIZE + 1][MAX_MEMO_SIZE]; // Check if a single cell is isolated int is_isolated(unsigned long matrix, int row, int col) { // Check if current cell is 0 if (!IS_BIT_SET(matrix, row * MATRIX_SIZE + col)) return 0; // Check left if (col > 0 && IS_BIT_SET(matrix, row * MATRIX_SIZE + col - 1)) return 0; // Check right if (col < MATRIX_SIZE - 1 && IS_BIT_SET(matrix, row * MATRIX_SIZE + col + 1)) return 0; // Check up if (row > 0 && IS_BIT_SET(matrix, (row - 1) * MATRIX_SIZE + col)) return 0; // Check down if (row < MATRIX_SIZE - 1 && IS_BIT_SET(matrix, (row + 1) * MATRIX_SIZE + col)) return 0; return 1; } // Check if the entire matrix is unholy int is_unholy(unsigned long matrix) { for (int row = 0; row < MATRIX_SIZE; row++) { for (int col = 0; col < MATRIX_SIZE; col++) { if (is_isolated(matrix, row, col)) return 0; } } return 1; } // Dynamic programming to count unholy matrices uint64_t count_unholy_matrices(int rows_left, unsigned long prev_rows) { // Base case: if we've filled all rows if (rows_left == 0) return 1; // Check memoization if (memo_init[rows_left][prev_rows]) return memo[rows_left][prev_rows]; uint64_t total_count = 0; // Try all possible next two rows for (unsigned long next_rows = 0; next_rows < (1ULL << (2 * MATRIX_SIZE)); next_rows++) { // Combine previous rows and new rows unsigned long current_matrix = (prev_rows << (2 * MATRIX_SIZE)) | next_rows; // Check if this configuration is unholy if (is_unholy(current_matrix)) { total_count += count_unholy_matrices(rows_left - 2, next_rows); } } // Memoize and return memo[rows_left][prev_rows] = total_count; memo_init[rows_left][prev_rows] = 1; return total_count; } int main() { // Clear memoization table memset(memo, 0, sizeof(memo)); memset(memo_init, 0, sizeof(memo_init)); // Start with 0 previous rows, 8 rows to fill uint64_t result = count_unholy_matrices(MATRIX_SIZE, 0); printf("Total number of unholy 8x8 matrices: %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.
Here's a comprehensive solution to the problem: Program Plan: 1. Define data structures and constants - Use unsigned long (64-bit) to represent 2 rows of 8 bits each - Create memoization cache for dynamic programming - Define constants for matrix size 2. Implement bit manipulation helper functions - is_isolated(): Check if a 1 is isolated - is_unholy(): Check if entire matrix is unholy 3. Implement dynamic programming solution - Recursive function with memoization - Base cases for small matrices - Enumerate all possible next two rows - Use memoization to cache intermediate results 4. Main function to drive computation and print result Here's the implementation:
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 | #include <stdio.h> #include <stdint.h> #include <string.h> #define ROWS 8 #define COLS 8 #define MEMO_SIZE (1ULL << 16) // Memoization cache uint64_t memo[ROWS + 1][MEMO_SIZE]; int memo_initialized = 0; // Check if a specific bit is isolated int is_isolated(uint64_t matrix, int row, int col) { // Check 4 cardinal directions int left = (col > 0 && (matrix & (1ULL << (row * COLS + col - 1)))); int right = (col < COLS - 1 && (matrix & (1ULL << (row * COLS + col + 1)))); int up = (row > 0 && (matrix & (1ULL << ((row - 1) * COLS + col)))); int down = (row < ROWS - 1 && (matrix & (1ULL << ((row + 1) * COLS + col)))); return !(left || right || up || down); } // Check if entire matrix is unholy (no isolated 1s) int is_unholy(uint64_t matrix) { for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { if (matrix & (1ULL << (row * COLS + col))) { if (is_isolated(matrix, row, col)) { return 0; // Found isolated 1 } } } } return 1; // No isolated 1s } // Dynamic programming solution with memoization uint64_t count_unholy_matrices(int rows_left, uint64_t prev_rows) { // Base case: completed matrix if (rows_left == 0) { return is_unholy(prev_rows) ? 1 : 0; } // Check memoization cache if (memo_initialized && memo[rows_left][prev_rows] != 0) { return memo[rows_left][prev_rows]; } uint64_t total_count = 0; // Enumerate all possible next two rows (2^16 possibilities) for (uint64_t next_rows = 0; next_rows < (1ULL << 16); next_rows++) { // Combine previous rows and next rows uint64_t current_matrix = prev_rows | (next_rows << (COLS * 2)); // Check if current matrix is valid so far if (is_unholy(current_matrix)) { // Recursively count remaining matrices total_count += count_unholy_matrices(rows_left - 2, next_rows); } } // Memoize result if (!memo_initialized) { memset(memo, 0, sizeof(memo)); memo_initialized = 1; } memo[rows_left][prev_rows] = total_count; return total_count; } int main() { uint64_t total_unholy_matrices = count_unholy_matrices(ROWS, 0); printf("Total unholy matrices: %llu\n", total_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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | #include <stdio.h> #include <stdint.h> #include <string.h> #define ROWS 8 #define COLS 8 #define MEMO_SIZE (1ULL << 16) // Memoization cache uint64_t memo[ROWS + 1][MEMO_SIZE]; int memo_initialized = 0; // Check if a specific bit is isolated int is_isolated(uint64_t matrix, int row, int col) { // Check 4 cardinal directions int left = (col > 0 && (matrix & (1ULL << (row * COLS + col - 1)))); int right = (col < COLS - 1 && (matrix & (1ULL << (row * COLS + col + 1)))); int up = (row > 0 && (matrix & (1ULL << ((row - 1) * COLS + col)))); int down = (row < ROWS - 1 && (matrix & (1ULL << ((row + 1) * COLS + col)))); return !(left || right || up || down); } // Check if entire matrix is unholy (no isolated 1s) int is_unholy(uint64_t matrix) { for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { if (matrix & (1ULL << (row * COLS + col))) { if (is_isolated(matrix, row, col)) { return 0; // Found isolated 1 } } } } return 1; // No isolated 1s } // Dynamic programming solution with memoization uint64_t count_unholy_matrices(int rows_left, uint64_t prev_rows) { // Base case: completed matrix if (rows_left == 0) { return is_unholy(prev_rows) ? 1 : 0; } // Check memoization cache if (memo_initialized && memo[rows_left][prev_rows] != 0) { return memo[rows_left][prev_rows]; } uint64_t total_count = 0; // Enumerate all possible next two rows (2^16 possibilities) for (uint64_t next_rows = 0; next_rows < (1ULL << 16); next_rows++) { // Combine previous rows and next rows uint64_t current_matrix = prev_rows | (next_rows << (COLS * 2)); // Check if current matrix is valid so far if (is_unholy(current_matrix)) { // Recursively count remaining matrices total_count += count_unholy_matrices(rows_left - 2, next_rows); } } // Memoize result if (!memo_initialized) { memset(memo, 0, sizeof(memo)); memo_initialized = 1; } memo[rows_left][prev_rows] = total_count; return total_count; } int main() { uint64_t total_unholy_matrices = count_unholy_matrices(ROWS, 0); printf("Total unholy matrices: %llu\n", total_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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | #include <stdio.h> #include <stdint.h> #include <string.h> #define ROWS 8 #define COLS 8 #define MEMO_SIZE (1ULL << 16) // Memoization cache uint64_t memo[ROWS + 1][MEMO_SIZE]; int memo_initialized = 0; // Check if a specific bit is isolated int is_isolated(uint64_t matrix, int row, int col) { // Check 4 cardinal directions int left = (col > 0 && (matrix & (1ULL << (row * COLS + col - 1)))); int right = (col < COLS - 1 && (matrix & (1ULL << (row * COLS + col + 1)))); int up = (row > 0 && (matrix & (1ULL << ((row - 1) * COLS + col)))); int down = (row < ROWS - 1 && (matrix & (1ULL << ((row + 1) * COLS + col)))); return !(left || right || up || down); } // Check if entire matrix is unholy (no isolated 1s) int is_unholy(uint64_t matrix) { for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { if (matrix & (1ULL << (row * COLS + col))) { if (is_isolated(matrix, row, col)) { return 0; // Found isolated 1 } } } } return 1; // No isolated 1s } // Dynamic programming solution with memoization uint64_t count_unholy_matrices(int rows_left, uint64_t prev_rows) { // Base case: completed matrix if (rows_left == 0) { return is_unholy(prev_rows) ? 1 : 0; } // Check memoization cache if (memo_initialized && memo[rows_left][prev_rows] != 0) { return memo[rows_left][prev_rows]; } uint64_t total_count = 0; // Enumerate all possible next two rows (2^16 possibilities) for (uint64_t next_rows = 0; next_rows < (1ULL << 16); next_rows++) { // Combine previous rows and next rows uint64_t current_matrix = prev_rows | (next_rows << (COLS * 2)); // Check if current matrix is valid so far if (is_unholy(current_matrix)) { // Recursively count remaining matrices total_count += count_unholy_matrices(rows_left - 2, next_rows); } } // Memoize result if (!memo_initialized) { memset(memo, 0, sizeof(memo)); memo_initialized = 1; } memo[rows_left][prev_rows] = total_count; return total_count; } int main() { uint64_t total_unholy_matrices = count_unholy_matrices(ROWS, 0); printf("Total unholy matrices: %llu\n", total_unholy_matrices); return 0; } |
Timeout: function took too long to complete
Testing if the previous output contains the string 1121231537486377866
: False