Last active
December 25, 2015 06:59
-
-
Save ezy023/6935988 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The row string is 396240001 | |
The col string is 000003000 | |
The box string is 396240001 | |
The all string is 396240001000003000396240001 | |
The row string is 39624�001 | |
The col string is 003000080 | |
The box string is 39624�001 | |
The all string is 39624�00100300008039624�001 | |
The row string is 39624�501 | |
The col string is 009401530 | |
The box string is 39624�501 | |
The all string is 39624�50100940153039624�501 | |
The row string is 9624�5711 | |
The col string is 900030150 | |
The box string is 39624�571 | |
The all string is 9624�571190003015039624�571 | |
The row string is 9624�5711 | |
The col string is 604705093 | |
The box string is 39624�571 | |
The all string is 9624�571160470509339624�571 | |
The row string is 9624�5711 | |
The col string is 208900605 | |
The box string is 39624�571 | |
The all string is 9624�571120890060539624�571 | |
The board string is 39624�571188060004504810390007950043030080000405023018010630059059070830003590007 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
// look into memcpy docs for stuff | |
void printString(char *string, char *descrip); | |
char *getRowValues(int index, char *board) | |
{ | |
int row = index / 9; | |
char *rowValues = malloc(10 * sizeof(char)); | |
memcpy(rowValues, &board[row], 9); | |
rowValues[9] = '\0'; | |
printString(rowValues, "row"); | |
return rowValues; | |
} | |
char *getColValues(int index, char *board) | |
{ | |
int col = index % 9; | |
char *colValues = malloc(10 * sizeof(char)); | |
for(int i = 0; i < 9; i++) { | |
int value = 9 * i + col; | |
memcpy(colValues+i, &board[value], 1); | |
} | |
colValues[9] = '\0'; | |
printString(colValues, "col"); | |
return colValues; | |
} | |
char *getBoxValues(int index, char *board) | |
{ | |
int boxIndices[9][9] = {0, 1, 2, 9, 10, 11, 18, 19, 20, 3, 4, 5, 12, 13, 14, 21, 22, 23, 6, 7, 8, 15, 16, 17, 24, 25, 26, 27, 28, 29, 36, 37, 38, 45, 46, 47, 30, 31, 32, 39, 40, 41, 48, 49, 50, 33, 34, 35, 42, 43, 44, 51, 52, 53, 54, 55, 56, 63, 64, 65, 72, 73, 74, 57, 58, 59, 66, 67, 68, 75, 76, 77, 60, 61, 62, 69, 70, 71, 78, 79, 80}; | |
int boxNumber = ((( index / 9) / 3) * 3) + (( index % 9 ) / 3); | |
char *boxValues = malloc(10 * sizeof(char)); | |
for(int i = 0; i < 9; i++) { | |
int value = boxIndices[boxNumber][i]; | |
memcpy(boxValues+i, &board[i], 1); | |
} | |
printString(boxValues, "box"); | |
return boxValues; | |
} | |
void printString(char *string, char *descrip) | |
{ | |
printf("The %s string is %s\n",descrip, string); | |
} | |
void printInt(int zero) | |
{ | |
printf("The integer is %d\n", zero); | |
} | |
int getNextZero(char *board) | |
{ | |
int zero = strcspn(board, "0"); | |
return zero; | |
} | |
char *getValues(int index, char *board) | |
{ | |
char *all_values = malloc(28 * sizeof(char)); | |
char *row_values = getRowValues(index, board); | |
char *col_values = getColValues(index, board); | |
char *box_values = getBoxValues(index, board); | |
strncpy(all_values, row_values, 9); | |
strncat(all_values, col_values, 9); | |
strncat(all_values, box_values, 9); | |
all_values[27] = '\0'; | |
printString(all_values, "all"); | |
return all_values; | |
} | |
char *getPossibilities(int index, char *board) | |
{ | |
char *numbers = "123456789"; | |
char *possibilities = malloc(10 * sizeof(char)); | |
char *values = getValues(index, board); | |
for(int i = 0; i < strlen(numbers); i++) { | |
char *checkInValues = strchr(values, numbers[i]); | |
if(checkInValues == NULL) { | |
possibilities[strlen(possibilities)] = numbers[i]; | |
} | |
} | |
possibilities[strlen(possibilities)] = '\0'; | |
return possibilities; | |
} | |
char *solve(char *board) | |
{ | |
int zero = strcspn(board, "0"); | |
if(zero == 81) { | |
return board; | |
} else { | |
char *possibilities = getPossibilities(zero, board); | |
if(possibilities != '\0') { | |
for(int i = 0; i < strlen(possibilities); i++) { | |
char *new_string = malloc(strlen(board) + 1); | |
memcpy(new_string, board, strlen(board) + 1); | |
new_string[zero] = possibilities[i]; | |
return solve(new_string); | |
} | |
} | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
char *the_board = "396240001100060004504810390007950043030080000405023018010630059059070830003590007"; | |
// printString(getRowValues(4, the_board)); | |
// printString(getColValues(4, the_board)); | |
// printString(getBoxValues(4, the_board)); | |
// printf("Full values below\n"); | |
// printString(getPossibilities(4, the_board)); | |
// char * poss = getPossibilities(4, the_board); | |
// printf("Possibilities length is %ld\n", strlen(poss)); | |
// printInt(getNextZero(the_board)); | |
printString(solve(the_board), "board"); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment