Created
December 9, 2022 06:16
-
-
Save silicontrip/15e5175460c8f4299499757b3f197cd5 to your computer and use it in GitHub Desktop.
sudoko solver, iterate only, no guessing.
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
#import <Foundation/Foundation.h> | |
void psetboard(NSSet * board) | |
{ | |
for (int y =0; y< 9 ; y++) | |
{ | |
for (int x = 0; x<9; x++) | |
{ | |
if ([board containsObject:@(y*9+x)]) | |
printf ("#"); | |
else | |
printf("."); | |
} | |
printf("\n"); | |
} | |
} | |
void pboard(int * board) | |
{ | |
for (int y =0; y< 9 ; y++) | |
{ | |
for (int x = 0; x<9; x++) | |
{ | |
if (board[y*9+x]>0) | |
printf ("%d",board[y*9+x]); | |
else | |
printf("."); | |
} | |
printf("\n"); | |
} | |
printf("\n"); | |
} | |
NSSet * uniq_row(int sq) | |
{ | |
int rw = sq / 9; | |
NSMutableSet * mset = [[[NSMutableSet alloc] init] autorelease]; | |
for (int x=0; x< 9; x++) | |
{ | |
int ss = rw*9 + x; | |
[mset addObject:@(ss)]; | |
} | |
// NSLog(@"square: %d row %@",sq,mset); | |
return [NSSet setWithSet:mset]; | |
} | |
NSSet * uniq_col(int sq) | |
{ | |
int cl = sq % 9; | |
NSMutableSet * mset = [[[NSMutableSet alloc] init] autorelease]; | |
for (int x=0; x< 9; x++) | |
{ | |
int ss = x * 9 + cl; | |
[mset addObject:@(ss)]; | |
} | |
// NSLog(@"square: %d col %@",sq,mset); | |
return [NSSet setWithSet:mset]; | |
} | |
NSSet * uniq_cell(int sq) | |
{ | |
NSMutableSet* mset = [[[NSMutableSet alloc] init] autorelease]; | |
int clx = (sq % 9) / 3; | |
int cly = sq / 27; | |
for (int y = 0; y<3; y++) | |
for (int x=0; x< 3; x++) | |
{ | |
int ss = clx * 3 + cly * 27 + y * 9 + x; | |
[mset addObject:@(ss)]; | |
} | |
// NSLog(@"square: %d cell %@",sq,mset); | |
return [NSSet setWithSet:mset]; | |
} | |
NSSet* uniq_squares(int sq) | |
{ | |
NSMutableSet* mset = [[[NSMutableSet alloc] init] autorelease]; | |
[mset unionSet:uniq_row(sq)]; | |
[mset unionSet:uniq_col(sq)]; | |
[mset unionSet:uniq_cell(sq)]; | |
//psetboard(mset); | |
return [NSSet setWithSet:mset]; | |
} | |
NSSet* uniq_numbers (int sq, int * board) | |
{ | |
NSSet* sqset = uniq_squares(sq); | |
NSMutableSet* numbers = [[[NSMutableSet alloc] init] autorelease]; | |
NSMutableSet* inumbers = [[[NSMutableSet alloc] init] autorelease]; | |
for (NSNumber* nn in sqset) | |
{ | |
if (nn > 0) | |
[numbers addObject:@(board[[nn intValue]])]; | |
} | |
//NSLog(@" sq: %d unum: %@",sq,numbers); | |
for (int i=1;i<10;i++) | |
{ | |
if (![numbers containsObject:@(i)]) | |
[inumbers addObject:@(i)]; | |
} | |
return [NSSet setWithSet:inumbers]; | |
} | |
//int * iterate (int * board) | |
void iterate (int * board) | |
{ | |
for (int sq =0; sq < 81; sq++) | |
{ | |
if (board[sq] == 0 ) | |
{ | |
// get uniq squares | |
NSSet* unum = uniq_numbers(sq,board); | |
// NSLog(@"%d: %lu : %@\n",sq,[unum count],unum); | |
if ([unum count] == 1) | |
board[sq] = [[unum anyObject] intValue]; | |
} | |
} | |
} | |
int main (int argc, const char* argv[]) | |
{ | |
int board[81]; | |
printf("argc: %d\n",argc); | |
if (argc==2) | |
{ | |
NSError* err; | |
NSString* fn = [NSString stringWithUTF8String:argv[1]]; | |
NSString* sboard = [NSString stringWithContentsOfFile:fn encoding:NSUTF8StringEncoding error:&err]; | |
NSArray* aboard = [sboard componentsSeparatedByString:@"\n"]; | |
// NSLog(@"%@",aboard); | |
int y = 0; | |
for (NSString* line in aboard) | |
{ | |
for(int x=0; x < [line length]; ++x) | |
{ | |
NSString* cc = [line substringWithRange:NSMakeRange(x, 1)]; | |
int sq = [cc intValue]; | |
board[y*9 + x] = sq; | |
if (sq>0) | |
printf("%d",sq); | |
else | |
printf (" "); | |
} | |
printf("\n"); | |
++y; | |
} | |
iterate(board); | |
pboard(board); | |
iterate(board); | |
pboard(board); | |
iterate(board); | |
pboard(board); | |
iterate(board); | |
pboard(board); | |
} | |
} |
Author
silicontrip
commented
Dec 9, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment