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
•
2....657.
49..27..1
......2.3
54..73.9.
7.6...3.4
.8.26..15
6.9......
8..94..36
.146....7
2022-12-09 20:07:14.400 a.out[45301:5291948] 1 Iteration
23...657.
49..27..1
1.....2.3
54..73.9.
726...3.4
.8326.715
6.9......
8..94.136
3146....7
2022-12-09 20:07:14.401 a.out[45301:5291948] 2 Iteration
238..657.
49..27..1
1.....2.3
541.73.9.
726...384
98326.715
6.9......
8..94.136
3146....7
2022-12-09 20:07:14.402 a.out[45301:5291948] 3 Iteration
238..6579
495.27.61
1.....2.3
541873692
726...384
983264715
6.9......
8..94.136
3146....7
2022-12-09 20:07:14.402 a.out[45301:5291948] 4 Iteration
238.16579
495327861
1.7...243
541873692
726...384
983264715
6.9.....8
8..94.136
3146....7
2022-12-09 20:07:14.403 a.out[45301:5291948] 5 Iteration
238416579
495327861
1675..243
541873692
726...384
983264715
6.9...4.8
8.294.136
3146..9.7
2022-12-09 20:07:14.404 a.out[45301:5291948] 6 Iteration
238416579
495327861
1675..243
541873692
7261..384
983264715
6.9...4.8
8.2945136
3146..9.7
2022-12-09 20:07:14.404 a.out[45301:5291948] 7 Iteration
238416579
495327861
1675..243
541873692
7261.9384
983264715
6.973.4.8
872945136
31468.9.7
2022-12-09 20:07:14.404 a.out[45301:5291948] 8 Iteration
238416579
495327861
167598243
541873692
726159384
983264715
65973.4.8
872945136
3146829.7
2022-12-09 20:07:14.404 a.out[45301:5291948] 9 Iteration
238416579
495327861
167598243
541873692
726159384
983264715
659731428
872945136
314682957
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment