r/dailyprogrammer • u/rya11111 3 1 • Jun 08 '12
[6/8/2012] Challenge #62 [difficult]
Write a program to solve 9x9 Sudoku puzzles.
Thanks to EvanHahn for the challenge at /r/dailyprogrammer_ideas .. If you have a challenge in your mind, head over there and post it!
4
u/skeeto -9 8 Jun 09 '12
C solution I wrote four years ago. It solves it in-place.
int solve(char matrix[9][9])
{
/* Find an empty spot. */
int x, y, i, j, s = 0;
for (i = 0; i < 9 && !s; i++)
for (j = 0; j < 9 && !s; j++)
if (matrix[i][j] == 0) {
x = i;
y = j;
s = 1;
}
/* No empty spots, we found a solution! */
if (!s)
return 1;
/* Determine legal numbers for this spot. */
char nums[10] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
for (i = 0, j = y; i < 9; i++)
nums[(int) matrix[i][j]] = 0; /* Vertically */
for (i = x, j = 0; j < 9; j++)
nums[(int) matrix[i][j]] = 0; /* Horizontally */
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
nums[(int) matrix[i + ((int) (x / 3)) * 3]
[j + ((int) (y / 3)) * 3]
] = 0; /* Within the partition */
/* Try each possible number and recurse for each. */
for (i = 1; i < 10; i++)
if (nums[i] > 0) {
matrix[x][y] = i;
if (solve(matrix))
return 1;
}
/* Each attempt failed: reset this position and report failure. */
matrix[x][y] = 0;
return 0;
}Solving the suggested puzzle:
char sample[9][9] =
{
{1, 0, 0, 0, 0, 7, 0, 9, 0},
{0, 3, 0, 0, 2, 0, 0, 0, 8},
{0, 0, 9, 6, 0, 0, 5, 0, 0},
{0, 0, 5, 3, 0, 0, 9, 0, 0},
{0, 1, 0, 0, 8, 0, 0, 0, 2},
{6, 0, 0, 0, 0, 4, 0, 0, 0},
{3, 0, 0, 0, 0, 0, 0, 1, 0},
{0, 4, 0, 0, 0, 0, 0, 0, 7},
{0, 0, 7, 0, 0, 0, 3, 0, 0}
};The time,
$ time ./a.out
real 0m0.003sThis worst case puzzle is pretty interesting, taking it almost 7 seconds.
char worst_case[9][9] =
{
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 3, 0, 8, 5},
{0, 0, 1, 0, 2, 0, 0, 0, 0},
{0, 0, 0, 5, 0, 7, 0, 0, 0},
{0, 0, 4, 0, 0, 0, 1, 0, 0},
{0, 9, 0, 0, 0, 0, 0, 0, 0},
{5, 0, 0, 0, 0, 0, 0, 7, 3},
{0, 0, 2, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 4, 0, 0, 0, 9}
};2
u/exor674 Jun 09 '12 edited Jun 09 '12
Mine on the worst case:
987 | 654 | 321 | 246 | 173 | 985 | 351 | 928 | 746 | 128 | 537 | 694 | 634 | 892 | 157 | 795 | 461 | 832 | 519 | 286 | 473 | 472 | 319 | 568 | 863 | 745 | 219 |real: 0m0.054s
Disabling the heuristic pass, real: 0m1.656s
3
u/exor674 Jun 08 '12 edited Jun 08 '12
C++11: ( the code for this is really horrible and badly designed, but it WORKS )
http://pastie.org/private/rg4apem3badsghgpfmoa7w
Combo of a very simple heuristic and then brute force.
( or see https://github.com/anall/libsudoku for a solver lib that can work on pretty much any kind of sudoku-ish puzzle -- 12x12, additional rules [ even odd ], etc... )
Edit: Following Cosmologicon's suggestion and using the same puzzle they solved:
162 | 857 | 493 |
534 | 129 | 678 |
789 | 643 | 521 |
475 | 312 | 986 |
913 | 586 | 742 |
628 | 794 | 135 |
356 | 478 | 219 |
241 | 935 | 867 |
897 | 261 | 354 |
real 0m0.014s
user 0m0.010s
sys 0m0.003s3
u/Cosmologicon 2 3 Jun 08 '12
Using an old solution I wrote with Algorithm X:
def algox(subsets, items = None):
if items is None: items = set().union(*subsets)
x = min(items, key = lambda z: sum(z in s for s in subsets))
for s0 in subsets:
if x not in s0: continue
nitems = items - set(s0)
if not nitems: return (s0,)
nsubsets = tuple(s for s in subsets if set(s0).isdisjoint(s))
a = algox(nsubsets, nitems)
if a: return (s0,) + a
return None
grid0 = "1....7.9..3..2...8..96..5....53..9...1..8...26....4...3......1..4......7..7...3.."
print grid0
sq = {} # Dictionary of all possible squares and their corresponding constraints
rcs = list((r, c, str(s)) for c in range(9) for r in range(9) for s in range(1,10))
sq = dict(((r, c, s), ((0, r, c), (1, r, s), (2, c, s), (3, r/3, c/3, s))) for r, c, s in rcs)
# Remove squares that conflict with the squares on the given grid
takens = [(r,c,s) for (r,c,s) in sq if grid0[9*r+c] == s]
tcons = set(sum((sq[t] for t in takens), ()))
sq = dict((s, cons) for s, cons in sq.items() if tcons.isdisjoint(cons))
a = algox(sq.values()) # Solve
grid = list(grid0)
for (r, c, s), cons in sq.items():
if cons in a: grid[9*r + c] = s
print "".join(grid)Suggestion: have people post the solution to a specific grid. Here's the one I'm solving, and it takes about 0.8 seconds.
3
u/wicked-canid 0 0 Jun 09 '12 edited Jun 09 '12
In Common Lisp: http://pastie.org/4053561
There must be a better solution without copying the grid so much: on the worst case grid, it allocates more than 1GB :D.
Note that CCL's GC is apparently quite efficient, since even when allocating 1GB, the execution doesn't spend that much time in GC...
Edit: here is a better solution that copies less: http://pastie.org/4053789
And the updated stats follow (on par with skeeto's for the worst case; who said Lisp was slow :)).
On Cosmologicon's grid:
1 6 2 | 8 5 7 | 4 9 3
5 3 4 | 1 2 9 | 6 7 8
7 8 9 | 6 4 3 | 5 2 1
------+-------+------
4 7 5 | 3 1 2 | 9 8 6
9 1 3 | 5 8 6 | 7 4 2
6 2 8 | 7 9 4 | 1 3 5
------+-------+------
3 5 6 | 4 7 8 | 2 1 9
2 4 1 | 9 3 5 | 8 6 7
8 9 7 | 2 6 1 | 3 5 4
(PRINC-GRID (SOLVE-GRID *COSMOLOGICON-GRID*))
took 23,868 microseconds (0.023868 seconds) to run.
1,877 microseconds (0.001877 seconds, 7.86%) of which was spent in GC.
During that period, and with 2 available CPU cores,
22,154 microseconds (0.022154 seconds) were spent in user mode
929 microseconds (0.000929 seconds) were spent in system mode
1,476,224 bytes of memory allocated.On skeeto's worst case grid:
9 8 7 | 6 5 4 | 3 2 1
2 4 6 | 1 7 3 | 9 8 5
3 5 1 | 9 2 8 | 7 4 6
------+-------+------
1 2 8 | 5 3 7 | 6 9 4
6 3 4 | 8 9 2 | 1 5 7
7 9 5 | 4 6 1 | 8 3 2
------+-------+------
5 1 9 | 2 8 6 | 4 7 3
4 7 2 | 3 1 9 | 5 6 8
8 6 3 | 7 4 5 | 2 1 9
(PRINC-GRID (SOLVE-GRID *WORST-CASE*))
took 6,829,994 microseconds (6.829994 seconds) to run.
436,434 microseconds (0.436434 seconds, 6.39%) of which was spent in GC.
During that period, and with 2 available CPU cores,
6,426,279 microseconds (6.426279 seconds) were spent in user mode
208,096 microseconds (0.208096 seconds) were spent in system mode
486,852,160 bytes of memory allocated.
1 minor page faults, 0 major page faults, 0 swaps.
5
u/[deleted] Jun 08 '12 edited Jun 08 '12
I actually wrote a solver about 6 months ago,
here is the solver class, pretty simple and brute force, but surprisingly quick:
Code
Edit: Cosmologicon's suggested puzzle: