Computer Science Canada

Logic Puzzle Challenge

Author:  wtd [ Sun Jul 24, 2005 5:53 pm ]
Post subject:  Logic Puzzle Challenge

I have a 9 by 9 grid.

Each space may contain a number between 1 and 9.

Each row and rolumn may only contain unique numbers.

The grid is further divided into 9 3 by 3 grids.

Each of these smaller grids may only contain unique numbers.

You start out with:

code:
*681*3***
*5**64*3*
2*****846
7**2**18*
1*56**7**
8**75***4
****8**52
*84*9***7
*72**63**


Where asterisks represent unknowns.

Write a computer program which computes the unknowns.

Author:  1of42 [ Sun Jul 24, 2005 6:09 pm ]
Post subject: 

O.M.F.G.

This is the puzzle that's sweepnig through Canada in the Globe, isn't it? (Name escapes me at the moment).

I'll have to give it a try though.

Author:  Hikaru79 [ Sun Jul 24, 2005 6:12 pm ]
Post subject: 

1of42, yes it is, but don't say the name because then people might be tempted to cheat by looking up existing algorithms. Security by obscurity Wink

I'm working on it too Very Happy

Author:  wtd [ Sun Jul 24, 2005 6:12 pm ]
Post subject: 

The example is the July 22nd puzzle. Smile

Oh, and the output should be the same as the input, but with the final values in place of asterisks.

Author:  Delos [ Sun Jul 24, 2005 6:13 pm ]
Post subject: 

I've solved a couple of them. Remind me of my precious Rubicks cube in many respects. Since they are all computer generated, I figure there must be an algorithm to *create* them, thus there ought to be one to solve them.
I have some ideas, though most are pretty long and tedious.

[mod:f88bf0f35e="wtd (I'm guessing)"]One word removed to maintain a fair competition.[/mod:f88bf0f35e]

Author:  wtd [ Sun Jul 24, 2005 6:17 pm ]
Post subject: 

I know it can be solved.

code:
$ wc grid_solver.py
 100  314 3174 grid_solver.py
$


Wink

Author:  MysticVegeta [ Mon Jul 25, 2005 9:15 am ]
Post subject:  Re: Logic Puzzle Challenge

wtd wrote:

Where asterisks represent unknowns.
Write a computer program which computes the unknowns.


What do you mean? We dont know the relations between the numbers, how to compute the asterisks??? Confused

Author:  wtd [ Mon Jul 25, 2005 11:24 am ]
Post subject:  Re: Logic Puzzle Challenge

MysticVegeta wrote:
wtd wrote:

Where asterisks represent unknowns.
Write a computer program which computes the unknowns.


What do you mean? We dont know the relations between the numbers, how to compute the asterisks??? Confused


The same number may not occur twice in any row, column, or smaller 3 by 3 grid.

Author:  rizzix [ Mon Jul 25, 2005 4:45 pm ]
Post subject: 

Aight, took me 17 whole min...

rizzix wrote:
__________________
|4-6-8 |1-2-3-|5-7-9-|
|9-5-7-|8-6-4-|2-3-1-|
|2-3-1-|9-7-5-|8-4-6-|
|~~~~|~~~~|~~~~|
|7-4-6-|2-3-9-|1-8-5-|
|1-9-5-|6-4-8-|7-2-3-|
|8-2-3-|7-5-1-|9-6-4-|
|~~~~|~~~~|~~~~|
|6-1-9-|3-8-7-|4-5-2-|
|3-8-4-|5-9-2-|6-1-7-|
|5-7-2-|4-1-6-|3-9-8-|
~~~~~~~~~~~~~~

Author:  wtd [ Mon Jul 25, 2005 4:48 pm ]
Post subject: 

Yes, I can solve it by had in around 10 minutes.

Now, show me the program you used to programmatically generate that answer. Smile

Author:  rizzix [ Mon Jul 25, 2005 4:51 pm ]
Post subject: 

i didn;t... it was by hand... Razz i'll work on an algorithm later.. right now i just did that to kill time, altough i got soo much stuff to do pfft...

Author:  Hikaru79 [ Mon Jul 25, 2005 8:34 pm ]
Post subject: 

I'm almost done my Python program. Awesome challenge, wtd, I really feel like my Python improved from doing this!

I'll send it to you as soon as it is done, wtd.

Author:  wtd [ Tue Jul 26, 2005 3:01 am ]
Post subject: 

Hikaru79 wrote:
I'm almost done my Python program. Awesome challenge, wtd, I really feel like my Python improved from doing this!

I'll send it to you as soon as it is done, wtd.


Great. I'm working on retooling mine to be more object-oriented... and... you know... work.

The messy one works. The clean, elegant one... not so much. Yet.

Author:  bugzpodder [ Tue Jul 26, 2005 9:41 pm ]
Post subject: 

i am assuming this is an attempt to show that python is much better than C++ in doing this. since i dont know python i'll have to stick to what i know. when i have time this weekend, I'll churn up something in C++. Stay tuned.

Author:  wtd [ Tue Jul 26, 2005 9:57 pm ]
Post subject: 

Any language works. I just picked Python because it has a handy feature in its standard library for this problem. No, I'm not going to tell you what the handy feature is, because that would give away how to solve the problem. Smile

Author:  bugzpodder [ Tue Jul 26, 2005 10:02 pm ]
Post subject: 

i figured so Wink

Author:  bugzpodder [ Tue Jul 26, 2005 10:46 pm ]
Post subject: 

code:

#include<iostream>
#include<string>
using namespace std;


int main(){
        int r[9],c[9],i,j,arr[9][9],rev[512];
        char ch;
        bool flag;
        memset(r,0,sizeof(r));  // row sum
        memset(c,0,sizeof(c)); // col sum
        for (i=0;i<9;i++)
                for (j=0;j<9;j++){
                        cin>>ch;
                        if (ch=='*')
                                arr[i][j]=-1;
                        else
                        {
                                arr[i][j]=ch-'1';  //0 based
                                r[i]+=1<<arr[i][j];  //sum row
                                c[j]+=1<<arr[i][j];  // sum col
                        }
                }
       
        for (i=0;i<512;i++)
                rev[i]=-1;          //reverse log table
        for (i=0;i<10;i++)
                rev[1<<i]=i;
        arr[0][0]=-1;   
        flag = true;
        int cnt=0;
        while(flag){
                flag = false;
                for (i=0;i<9 && !flag;i++)
                        for (j=0;j<9 && !flag;j++){
                                int tmp = 511-(r[i]|c[j]);
                                if (rev[tmp]>=0)
                                {
                                        arr[i][j]=rev[tmp];
                                        flag=true;
                                        r[i]+=tmp;
                                        c[j]+=tmp;
                                }
                        }
        for (i=0;i<9;i++){
                for (j=0;j<9;j++)
                        cout<<arr[i][j]+1;
                cout<<endl;
                }
        cout<<endl;
        for (i=0;i<9;i++) cout<<r[i]<<" "; cout<<endl;
        for (i=0;i<9;i++) cout<<c[i]<<" "; cout<<endl;
        }
       
       
                       
        return 0;
}


this doesnt work yet... too tired to debug at night. continue tomorrow
I bet this is already twice as long as your python thingy

Author:  wtd [ Wed Jul 27, 2005 1:26 pm ]
Post subject: 

c++:
int r[9],c[9],i,j,arr[9][9],rev[512];
   char ch;
   bool flag;
   memset(r,0,sizeof(r))// row sum
   memset(c,0,sizeof(c)); // col sum
   for (i=0;i<9;i++)
      for (j=0;j<9;j++){
         cin>>ch;
         if (ch=='*')
            arr[i][j]=-1;
         else
         {
            arr[i][j]=ch-'1'//0 based
            r[i]+=1<<arr[i][j]//sum row
            c[j]+=1<<arr[i][j]// sum col
         }
      }


I'm not even going to evaluate code that uses such horrendous variable naming, lacks decent use of whitespace, makes use of C-isms like memset (for which there is no readily apparent include), makes inconsistent use of braces, and uses multidimensional arrays instead of a matrix class.

Smile

P.S. That is shorter than my Python program. That is not a virtue.

Author:  1of42 [ Wed Jul 27, 2005 2:13 pm ]
Post subject: 

You know, wtd, one of these days somebody is going to snap when they hear "whitespace" one more time and go on a massive killing spree. Laughing

Author:  wtd [ Wed Jul 27, 2005 2:35 pm ]
Post subject: 

1of42 wrote:
You know, wtd, one of these days somebody is going to snap when they hear "whitespace" one more time and go on a massive killing spree. Laughing


As soon as people start using whitespace appropriately in their programs, I'll shut up about it.

Author:  bugzpodder [ Wed Jul 27, 2005 8:57 pm ]
Post subject: 

oh come on, i've tried... i've even added comments!! and i indented!! at first i declared global variables. i thought you'd say something about that so i even moved them into local variables.

now back to debugging...
ps i am using notepad...

Author:  wtd [ Wed Jul 27, 2005 9:11 pm ]
Post subject: 

bugzpodder wrote:
oh come on, i've tried... i've even added comments!!


Don't use comments when you can write self-explanatory code.

Key to this is not writing your own code when the library contains a function that already does what you want.

c++:
#include <iostream>
#include <algorithm>

int main()
{
   int arrayOfInts[5] = {34, 65, 76, 12, 98};
   int secondArray[5];

   std::reverse_copy(arrayOfInts, arrayOfInts + 5, secondArray);

   std::cout << "First Array:" << std::endl;
   for (int arrayIndex(0); arrayIndex < 5; ++arrayIndex)
      std::cout << arrayOfInts[arrayIndex] << std::endl;

   std::cout << "Second Array:" << std::endl;
   for (int arrayIndex(0); arrayIndex < 5; ++arrayIndex)
      std::cout << secondArray[arrayIndex] << std::endl;

   return 0;
}


bugzpodder wrote:
ps i am using notepad...


Don't. Use Textpad, if you have to use Windows.

Author:  Delos [ Wed Jul 27, 2005 10:54 pm ]
Post subject: 

Well, I have an awful programme working under OOT. It's very, very primitive, baically bashing away random numbers until it finds a solution. I was able to optimize it a little, but not too much. So far it can solve a puzzle with 8 missing spaces in between half and five seconds. Not worth it.

I've been thinking about a more elegant algorithm, but haven't found anything yet. I will eventually, though. I'm slowly starting to see some patterns. Of course as far as I know they're the wrong ones, but bah.

Author:  wtd [ Wed Jul 27, 2005 11:02 pm ]
Post subject: 

A hint for everyone, since it's been a little while:

For each square that has no known value, you can generate a list of possibilities by starting with the set of numbers (1, 2, 3, 4, 5, 6, 7, 8, 9) and removing anything that it can't be.

Once you do this, you may notice that there are spaces which have only a single possibility.

Author:  Catalyst [ Thu Jul 28, 2005 1:10 am ]
Post subject: 

kinda verbose but it works

Author:  wtd [ Thu Jul 28, 2005 3:56 am ]
Post subject: 

Tsk tsk...

c++:
      int count=0;
      for (int i=0;i<9;i++) {
         for (int k=0;k<9;k++) {
            if (p_data[i][k].count_p()==1 && data[i][k]==0) {
               data[i][k]=p_data[i][k].get_single_value();
            }
            if (data[i][k]==0) {
               count++;   //count # of unknowns left
            }
         }
      }

Author:  Catalyst [ Thu Jul 28, 2005 11:45 pm ]
Post subject: 

is it incorrect or just not as clean as it could be?

Author:  wtd [ Thu Jul 28, 2005 11:58 pm ]
Post subject: 

Catalyst wrote:
is it incorrect or just not as clean as it could be?


I have no idea if it's incorrect, but it's horrendous code. You need to use meaningful variable names. Smile

Author:  1of42 [ Fri Jul 29, 2005 12:08 am ]
Post subject: 

Honestly, I think wtd just made this thread so he could correct some more people ont heir variable names. Rolling Eyes

(yes, i'm joking)

Author:  wtd [ Fri Jul 29, 2005 12:19 am ]
Post subject: 

As I said... I'll stop harping on it when people actually start listening to me. Smile

Author:  Catalyst [ Fri Jul 29, 2005 12:33 am ]
Post subject: 

while the code could be a bit more elegant and the names longer and more meaningful I didn't think of putting in the effort of cleaning it up when not many people would read the code

Author:  wtd [ Fri Jul 29, 2005 12:38 am ]
Post subject: 

And still not good enough. Wink

"row", "col"... do these refer to an index, or some kind of data structure that stores a row or column?

Author:  MysticVegeta [ Fri Jul 29, 2005 7:40 am ]
Post subject: 

I am trying to code this in turing Embarassed lets see if i make it, now i know 1 thing atleast, even if it works but doesnt have proper varible names/structure, if doesnt qualify for wtd's marking Laughing

Author:  Catalyst [ Fri Jul 29, 2005 1:09 pm ]
Post subject: 

oh well it happens Laughing

Author:  bugzpodder [ Sat Jul 30, 2005 8:27 pm ]
Post subject: 

lol after reading zylum's solution I realized that I didnt read the question properly. I didnt know each 3x3 square contained only unique digits!

Author:  wtd [ Sat Jul 30, 2005 10:57 pm ]
Post subject: 

Anybody who doesn't want to see my Python code for this... say so now!

I'm ready to post it. Smile

Author:  Hikaru79 [ Sun Jul 31, 2005 2:13 am ]
Post subject: 

Ack, just in time! I've been done for a few days now, but it was on a different computer so I kept putting off posting it.

Here it is, and just for the record, I had thought of wtd's suggestion before it was given.

Note: The forum doesn't allow uploading .py so just rename it before executing.

Another note: Both of these are with UNIX line breaks, so use something like ScITE or vim if the whole thing looks like one long line to you.

Author:  wtd [ Sun Jul 31, 2005 4:19 am ]
Post subject: 

Hikaru79, this:

Python:
combinationList = []
for xCoord in range(0, 9):
   if getSolvedValue(xCoord, yCoord) != None and combinationList.count(getSolvedValue(xCoord, yCoord)) == 0:
      combinationList.append(getSolvedValue(xCoord, yCoord))
return combinationList


Can be written as:

Python:
return set(getSolvedValue(xCoord, yCoord) for xCoord in range(0, 9) if getSolvedValue(xCoord, yCoord) != None)

Author:  rizzix [ Sun Jul 31, 2005 10:47 am ]
Post subject: 

what version of python do i need to use to run your script? i can't get it to work (using python2.2)

i got mine done, but i won't be posting it, yet... it can solve the puzzle. but.. well there's some issue i need to attend to...

so far i've seen this issue in Hikaru's and Catalysts' solution. i simply can't test it on yours. i can't run it.

Author:  Hikaru79 [ Sun Jul 31, 2005 12:16 pm ]
Post subject: 

rizzix wrote:
what version of python do i need to use to run your script? i can't get it to work (using python2.2)

i got mine done, but i won't be posting it, yet... it can solve the puzzle. but.. well there's some issue i need to attend to...

so far i've seen this issue in Hikaru's and Catalysts' solution. i simply can't test it on yours. i can't run it.

It works fine for me (Python 2.4). Make sure you pass the filename of the puzzle as an argument. (I'm assuming you were reffering to wtd's program)

What issue did mine and Catalyst's have? Sad

Author:  wtd [ Sun Jul 31, 2005 1:39 pm ]
Post subject: 

Yes. My program takes advantages of features that only exist in Python 2.4.

Author:  Hikaru79 [ Sun Jul 31, 2005 2:21 pm ]
Post subject: 

Well, guys, here's an extension to wtd's excellent challenge! Our programs aren't perfect yet Smile

Here, for example, is a puzzle that none of the programs submitted so far can solve:

code:
4***13**8
*587**1*4
1****49**
89*2*74*1
*41***27*
7*2451*89
*173**846
**4178592
28*64**1*

Author:  wtd [ Sun Jul 31, 2005 3:36 pm ]
Post subject: 

Does anyone think there might be a recursive algorithm that could be beneficial?

Author:  wtd [ Sun Jul 31, 2005 3:37 pm ]
Post subject: 

I also suspect this would be blindingly obvious if my Prolog skills were better. Smile

Author:  wtd [ Sun Jul 31, 2005 3:47 pm ]
Post subject: 

I think I may have challenged myself a bit too well on this one.

Author:  wtd [ Sun Jul 31, 2005 3:49 pm ]
Post subject: 

Hikaru79 wrote:
Well, guys, here's an extension to wtd's excellent challenge! Our programs aren't perfect yet Smile

Here, for example, is a puzzle that none of the programs submitted so far can solve:

code:
4***13**8
*587**1*4
1****49**
89*2*74*1
*41***27*
7*2451*89
*173**846
**4178592
28*64**1*


Have you done this one by hand? I'm having a lot of trouble finding a solution that way.

Author:  Hikaru79 [ Sun Jul 31, 2005 4:05 pm ]
Post subject: 

Yes, it's considered pretty difficult Smile That's why I chose it.

Here's the actual solution, if it helps any:
code:
429513768
658792134
173864925
896237451
541986273
732451689
917325846
364178592
285649317

Author:  wtd [ Sun Jul 31, 2005 4:17 pm ]
Post subject: 

Maybe it makes me stupid, but I can see no conceivable way to solve this puzzle.

Author:  Hikaru79 [ Sun Jul 31, 2005 4:22 pm ]
Post subject: 

Here is an analysis of the puzzle, which is considered to be quite hard even by the standards of people who have sizable post counts on a forum about Sudoku. (I guess its safe to let the cat out of the bag now Razz) They discuss the technique used. Scroll down for definitions of the terms in Tso's first post, since the poster didn't understand exactly what was said.

My little scriptlet is going to need a complete overhaul ^_^; I think I'm going to go the OOP route too, if I'm going to make one that can solve any of these more difficult ones.

Author:  MysticVegeta [ Sun Jul 31, 2005 5:42 pm ]
Post subject: 

is the turing solution still on? I am almost done with it, minor fixations. (no i didnt cheat/read the algorithm)

by the ways, shouldnt algorithm be spelled alogrithm. People usually say "alog". Algo is more like "al go there" as in a phrase. Or maybe i am just dumb Embarassed

Author:  wtd [ Sun Jul 31, 2005 5:48 pm ]
Post subject: 

MysticVegeta wrote:
is the turing solution still on? I am almost done with it, minor fixations. (no i didnt cheat/read the algorithm)

by the ways, shouldnt algorithm be spelled alogrithm. People usually say "alog". Algo is more like "al go there" as in a phrase. Or maybe i am just dumb Embarassed


You're welcome to solve it in watever way you see fit, as long as it works. Smile

Author:  Delos [ Sun Jul 31, 2005 10:10 pm ]
Post subject: 

MysticVegeta wrote:
by the ways, shouldnt algorithm be spelled alogrithm. People usually say "alog". Algo is more like "al go there" as in a phrase. Or maybe i am just dumb Embarassed


I'm not sure who you've been speaking to, but I've never heard anyone pronounce algorithm as "al-og-rithm". Check here for the pronouciation.

Author:  Hikaru79 [ Sun Jul 31, 2005 10:20 pm ]
Post subject: 

Yup, it's "algorithm" ... interesting fact: the word is based on the name of the Persian Mathematician Al-Khwarizmi (pronounce it out -- it sounds like algorithm!) who also happens to be the guy who wrote the "al-Kitab al-mukhtasar fi hisab al-jabr wa'l-muqabala (حساب الجبر و المقابلة)". That word in the middle "al-jabr" is where our word for "Algebra" comes from. This guy, which the word "algorithm" is named after, is actually the father of algebra! Very Happy

Just thought that was neat. Sorry for the hijack Embarassed

Author:  zylum [ Mon Aug 01, 2005 7:29 am ]
Post subject: 

haha i win... without cheating too!!!

code:
import java.util.*;
import java.io.*;
import java.lang.*;

class Grid {
        public static int[][] grid = new int[9][9];
       
        public static void main(String[] args) throws IOException {
                Vector[][] Vgrid = new Vector[9][9];
               
                for (int i = 0; i < 9; i ++)for (int j = 0; j < 9; j ++) Vgrid[i][j] = new Vector();
               
        BufferedReader input = new BufferedReader(new FileReader("grid.txt"));
        for (int r = 0; r < 9; r ++) {
            String str = input.readLine();
            for (int c = 0; c < 9; c ++) {
                if (str.charAt(c) == '*') {
                    grid[c][r] = -1;
                    for (int i = 1; i <= 9; i++) Vgrid[c][r].add(new Integer(i));
                } else {
                    grid[c][r] = str.charAt(c) - '0';
                    Vgrid[c][r].add(new Integer(grid[c][r]));
                }
            }
        }
       
        for (int c = 0; c < 9; c ++) {
            for (int r = 0; r < 9; r ++) {
                if (grid[c][r] < 0) {
                    for (int i = 0; i < 9; i ++) {
                        Vgrid[c][r].remove(new Integer(grid[i][r]));
                        Vgrid[c][r].remove(new Integer(grid[c][i]));
                    }
                    for (int i = 0; i < 3; i ++) for (int j = 0; j < 3; j ++) {
                        Vgrid[c][r].remove(new Integer(grid[((int)c/3)*3 + i][((int)r/3)*3 + j]));
                    }
                }
            }
        }
            
        Solve (Vgrid, 0, 0);
    }

    static void Solve (Vector[][] Vgrid, int c, int r) {
        if (r == 9) {
            for (int row = 0; row < 9; row ++) {
                String str = "";
                for (int col = 0; col < 9; col ++) {
                    str += grid[col][row] + " ";
                }
                System.out.println(str);
            }

         System.out.println("");
            return;
        }
       
        Vector[][] Tgrid = new Vector[9][9];
                for (int m = 0; m < 9; m ++) for (int n = 0; n < 9; n ++) Tgrid[m][n] = (Vector) Vgrid[m][n].clone();

        if (grid[c][r] < 0) {
            for (int i = 0; i < Tgrid[c][r].size(); i ++) { 
                      
                grid[c][r] = ((Integer)Tgrid[c][r].elementAt(i)).intValue();
                Tgrid[c][r].remove(new Integer(grid[c][r]));
               
                for (int j = 0; j < 9; j ++) {
                    if (grid[j][r] < 0) Tgrid[j][r].remove(new Integer(grid[c][r]));
                    if (grid[c][j] < 0) Tgrid[c][j].remove(new Integer(grid[c][r]));
                }
               
                for (int j = 0; j < 3; j ++) for (int k = 0; k < 3; k ++) {
                    Tgrid[((int)c/3)*3 + j][((int)r/3)*3 + k].remove(new Integer(grid[c][r]));
                }
               
                if (c < 8) Solve (Tgrid, c+1, r);
                else Solve (Tgrid, 0, r+1);
               
                grid[c][r] = -1;
                for (int m = 0; m < 9; m ++) for (int n = 0; n < 9; n ++) Tgrid[m][n] = (Vector) Vgrid[m][n].clone();
            }
        } else {
            if (c < 8) Solve (Tgrid, c+1, r);
            else  Solve (Tgrid, 0, r+1);
        }
    }
}


thank you recursion! this problem is actaully very easy if you think about it... what i have is a 9x9 array of vectors which store the possible values of that location. i also have an array of ints which keeps track of which values are unknown. then you recursively try every single combination of numbers each time removing numbers from the possiblilities.

i was at first trying to get this to work with turing but it would be hard to do without vectors...

also while working on this i was trying to clone the array of vectors like:

Tgrid = Vgrid.clone();

which Tgrid and Vgrid are both 2d arrays of vectors... the problem is that i only make a shallow copy of the 2d array, therefore im sending the direct reference of the original vectors to the new vectors. this means when i change the new vectors, so do the old ones... after many frustrating hours if finally realized this and fixed it by looping through the arrays and cloning each individual vector Mad

Author:  MysticVegeta [ Mon Aug 01, 2005 9:01 am ]
Post subject: 

nono, dont post the turing solution, genius. Save that for me and people my level Sad I dont know **** about the vectors (except the meaning in physics) in programming so i cant read your java code. I am not cheating!!!!!!!! Mad

Author:  Hikaru79 [ Mon Aug 01, 2005 11:07 am ]
Post subject: 

Zylum, your program does some very wierd stuff. For example, I put in the tough puzzle that nobody has solved yet:
code:
4***13**8
*587**1*4
1****49**
89*2*74*1
*41***27*
7*2451*89
*173**846
**4178592
28*64**1*

And yours spits out:
code:
4 6 1 8 5 7 9 3 2
2 5 7 9 4 3 1 6 8
9 8 3 6 1 2 7 4 5
5 7 8 2 9 4 3 1 6
1 9 6 3 8 5 2 7 4
3 2 4 7 6 1 5 8 9
7 1 9 4 2 6 8 5 3
6 3 2 5 7 8 4 9 1
8 4 5 1 3 9 6 2 7

Well, that's a legal board position and all, but ... your program has switched around the clues! Almost all of them have disappeared...

I tried it with a bunch of different puzzle files, and they all do this -- your program never gives a correct answer! It always just gives legal positions that do not match the clues that were given in the original puzzle Sad

Another example: Your program thinks this is the solution the wtd's original posted puzzle:
code:
4 9 2 7 1 8 6 3 5
6 5 3 4 9 2 1 8 7
8 7 1 6 5 3 9 4 2
1 8 9 2 6 7 3 5 4
2 6 7 3 4 5 8 9 1
3 4 5 9 8 1 7 2 6
5 2 8 1 7 9 4 6 3
7 3 4 8 2 6 5 1 9
9 1 6 5 3 4 2 7 8

Author:  rizzix [ Mon Aug 01, 2005 1:53 pm ]
Post subject: 

YEs this is the issue i'm talking about.. i have a vague idea of a solution, its only a matter of implementing it.

Hint: bla bla bla.. yes there was a hint here but I deleted, cuz i'm soo stupidly selfish and it gives away the solution anyways.. Laughing

Author:  Hikaru79 [ Mon Aug 01, 2005 6:42 pm ]
Post subject: 

rizzix wrote:
ROFL! solved! can't post it yet though... hehe i want those prizes from thestar.com!!! Laughing

An iPod is it?

Neat! Smile Can it solve the extension one I posted too?

what prizes? maybe i should hide my solution too Twisted Evil

Author:  rizzix [ Mon Aug 01, 2005 8:24 pm ]
Post subject: 

oooh a new case eh? nope dosen't seem like it Evil or Very Mad... anyways try running ur programs against this one:
code:
            6,7,0, 0,5,0, 0,2,3,
            0,0,0, 2,0,9, 0,0,0,
            0,0,9, 0,4,0, 8,0,0,
             
            3,6,0, 0,0,0, 0,5,2,
            0,0,0, 0,0,0, 0,0,0,
            7,5,0, 0,0,0, 0,4,9,
           
            0,0,7, 0,6,0, 1,0,0,
            0,0,0, 9,0,4, 0,0,0,
            9,4,0, 0,2,0, 0,8,6
where 0s represent __blanks__


as for your puzzle here's up to how much my program can solve before quitting: Confused
code:
4  2  9    5  1  3    7  -  8   
 3  5  8    7  2  9    1  6  4   
 1  7  6    8  -  4    9  2  3   

 8  9  3    2  6  7    4  5  1   
 5  4  1    9  8  -    2  7  -   
 7  6  2    4  5  1    3  8  9   

 9  1  7    3  -  2    8  4  6   
 6  3  4    1  7  8    5  9  2   
 2  8  5    6  4  -    -  1  7

Author:  zylum [ Mon Aug 01, 2005 8:58 pm ]
Post subject: 

ok why you guys have to go scare me like that? lol when i was printing the solution i mixed up my col and row vars so it was printing the answer rotated 90 degrees lol. i made last minute changes before posting (better variable names) and missed the mistake. anyways its fixed now. the solution it gives me to the one rizzix posted is:

code:
674851923
835279461
129346875
361498752
492735618
758612349
287563194
516984237
943127586


how could you guys ever doubt me? Laughing

anyways, an interesting thing you can do with my program is enter all asterisks and it will spit out all possible ways the numbers can be arranged. There seems to be quite a few..

Author:  rizzix [ Mon Aug 01, 2005 11:08 pm ]
Post subject: 

nice zylum, but a couple of things...

#1) your algorithm is the basic trial & error. It works, but it is the most inefficient. Nevertheless, congrants for being the first one to come up with a _working_ solution!

#2) man... tidy up!! took me a while to figure out what ur doin... good job though! Eitherway, more meaningful names would have helped a lot. Also, a more OO approach would have cleaned things up pretty good.

#) yea congrants again for being the first..

Author:  Hikaru79 [ Tue Aug 02, 2005 12:01 am ]
Post subject: 

Whoo, good job, zylum Smile It may be brute force, but yours is the only one to crack part 2.

+50 BITS!

Edit: Aw, crap =/ You're a mod. Well, it's the thought that counts right? Embarassed

Author:  rizzix [ Tue Aug 02, 2005 9:52 am ]
Post subject: 

mine passes part2 as well.. but only if it invovles a little trial & error. And no it's not the same as his.

ok i've attached it...

to compile & run using ant:
  1. extract the files
  2. open the terminal
  3. cd to the Sudoku project folder
  4. type "ant" in terminal to build the project
  5. type "ant PuzzleSolver" to run the PuzzleSlover program

Author:  MysticVegeta [ Tue Aug 02, 2005 2:08 pm ]
Post subject: 

zylum wrote:

anyways, an interesting thing you can do with my program is enter all asterisks and it will spit out all possible ways the numbers can be arranged. There seems to be quite a few..


Then that means your program has a fault. There are more than 1,000,000,000 possible ways made uptill now. Read it on the sudoku newspaper. Smile Well but you are still the first one, good job Smile Wait till i get my turing solution out Twisted Evil

Edit : Just found.

There are 6,670,903,752,021,072,936,960 possibilites.

Executes forever.

Author:  zylum [ Tue Aug 02, 2005 8:16 pm ]
Post subject: 

MysticVegeta wrote:
zylum wrote:

anyways, an interesting thing you can do with my program is enter all asterisks and it will spit out all possible ways the numbers can be arranged. There seems to be quite a few..


Then that means your program has a fault. There are more than 1,000,000,000 possible ways made uptill now. Read it on the sudoku newspaper. Smile Well but you are still the first one, good job Smile Wait till i get my turing solution out Twisted Evil

Edit : Just found.

There are 6,670,903,752,021,072,936,960 possibilites.

Executes forever.


how is my program at fault? i never said how many solutions there were, all i said is there are quite a few. meaning i ran the program for like 2 minutes and it was still giving me solutions.

Author:  MysticVegeta [ Wed Aug 03, 2005 9:06 am ]
Post subject: 

ic ic, sry about that. I thought it told you the possible ways. Good job! Execute it for 1 hour and sell the puzzle to the local newspaper. Very Happy

Author:  wtd [ Thu Aug 18, 2005 2:39 pm ]
Post subject: 

Still working on this, and not brute-forcing it. The fiendish game I'm working with is:

code:
  9 8
6 8 4   9
53   67
497  3 2
   2 1
 5 8  396
  47   53
7   3 4 8
    6 9


So far I can get it to:

code:
?49?87??5
678?4???9
53?9?6784
497653821
???291547
?5?874396
9?47???53
72??394?8
???46?972


My code thus far is attached. Yes, there's lots of reduce voodoo involving sets. Smile

I've implemented three solving techniques thus far. The simplest is to use the knowns to eliminate possibilities.

The next technique is to look for unique unknowns. If only one square in a row, column, or zone has a possibility, then that must be the value of the square.

The next technique looks at unknowns again, and determines if there are cases where you can determine that a certain number must be in a particular row or column within a zone. If so, then that number isn't in the corresponding row or column in other zones.

Author:  MysticVegeta [ Sat Aug 20, 2005 12:03 pm ]
Post subject: 

I am getting nowhere with the turing solution but i am trying hard...

Author:  wtd [ Sat Aug 20, 2005 4:23 pm ]
Post subject: 

MysticVegeta wrote:
I am getting nowhere with the turing solution but i am trying hard...


I can imagine very few other languages that would make this as tedious as Turing. For instance, I find myself very frequently making use of lists or in other words, "dynamically sized collections": something that might have any number of items. Turing does not make dealing with such easy, and there are enough complications in this problem without wasting so much time on tedium.

Author:  zylum [ Tue Aug 30, 2005 3:24 am ]
Post subject: 

here's a turing solution:

code:
var initialPossible : array 1 .. 9, 1 .. 9, 1 .. 9 of boolean
var grid : array 1 .. 9, 1 .. 9 of int

proc loadGrid (fileName : string)

    var file : int
    var line : string

    open : file, fileName, get

    for row : 1 .. 9
        get : file, line
        for col : 1 .. 9
            if line (col) = "*" then
                grid (col, row) := -1
                for i : 1 .. 9
                    initialPossible (col, row, i) := true
                end for
            else
                grid (col, row) := strint (line (col))
                for i : 1 .. 9
                    initialPossible (col, row, i) := false
                end for
            end if
        end for
    end for

    close (file)

    for col : 1 .. 9
        for row : 1 .. 9
            if grid (col, row) < 0 then
                for i : 1 .. 9
                    if grid (i, row) > 0 then
                        initialPossible (col, row, grid (i, row)) := false
                    end if
                    if grid (col, i) > 0 then
                        initialPossible (col, row, grid (col, i)) := false
                    end if
                end for
                for i : 1 .. 3
                    for j : 1 .. 3
                        if grid (((col - 1) div 3) * 3 + i, ((row - 1) div 3) * 3 + j) > 0 then
                            initialPossible (col, row, grid (((col - 1) div 3) * 3 + i, ((row - 1) div 3) * 3 + j)) := false
                        end if
                    end for
                end for
            end if
        end for
    end for

end loadGrid

proc solve (possible : array 1 .. 9, 1 .. 9, 1 .. 9 of boolean, c, r : int)

    if r = 10 then
        for row : 1 .. 9
            for col : 1 .. 9
                put grid (col, row), " " ..
            end for
            put ""
        end for
        return
    end if

    if grid (c, r) < 0 then
        var tempPossible := possible

        for i : 1 .. 9
            if tempPossible (c, r, i) then
                grid (c, r) := i

                for j : 1 .. 9
                    tempPossible (j, r, grid (c, r)) := false
                    tempPossible (c, j, grid (c, r)) := false
                end for

                for j : 1 .. 3
                    for k : 1 .. 3
                        tempPossible (((c - 1) div 3) * 3 + j, ((r - 1) div 3) * 3 + k, grid (c, r)) := false
                    end for
                end for

                if c < 9 then
                    solve (tempPossible, c + 1, r)
                else
                    solve (tempPossible, 1, r + 1)
                end if

                tempPossible := possible

                grid (c, r) := -1
            end if
        end for
    else
        if c < 9 then
            solve (possible, c + 1, r)
        else
            solve (possible, 1, r + 1)
        end if
    end if

end solve

loadGrid ("grid2.txt")
solve (initialPossible, 1, 1)

put "\n\n", Time.Elapsed


have fun Wink

Author:  rizzix [ Tue Nov 29, 2005 12:26 pm ]
Post subject: 

Hi there guys... just to bring back this good old topic so as to let you know.. my algorithm fails against the Top95 sudoku puzzles (well at least against the 1st two anyway):

Here, test your solutions against these:
Top 95 and the so-called Impossible 520

Edit: I'm tying running zylum's solution against the 2nd of the Top95 and.. well on my old 450mhz cpu.. it's taking a really long time.. although i'm pretty sure it will solve it... (right?).. at some point anyway..

Author:  rizzix [ Tue Nov 29, 2005 1:08 pm ]
Post subject: 

finally! solved! (took around 40min):

solution to the 2nd one:
code:
5 2 7 3 1 6 4 8 9
8 9 6 5 4 2 7 3 1
3 1 4 9 8 7 5 6 2
1 7 2 4 5 3 8 9 6
6 8 9 2 7 1 3 5 4
4 5 3 6 9 8 2 1 7
9 4 1 8 2 5 6 7 3
7 6 5 1 3 4 9 2 8
2 3 8 7 6 9 1 4 5


...it was solved using zylum's code btw.

Author:  wtd [ Tue Nov 29, 2005 1:35 pm ]
Post subject: 

I've been continuing to work on this using Python, and I have two possibility reduction techniques successfully implemented in my new, cleaner codebase.

They're fairly simple. The first simply removes any possibilities that already occur in the row, column or zone (3x3 square). The second checks to see if an unsolved cell contains a possibility that is not present in the rest of the row, column, or zone.

I can get from:

code:
92 |83 |
   |   |
  1| 7 |85
---+---+---
13 |   |  8
  8| 4 |6
4  |   | 97
---+---+---
 16| 8 |4
   |   |
   | 17| 69


To:

code:
92 |83 |
   |   |9
 41| 7 |85
---+---+---
13 |   | 48
  8| 4 |6
46 |  8| 97
---+---+---
 16| 8 |4
   |   | 8
   | 17| 69


Without guessing.

A third strategy would allow me to proceed further, but I have not yet been able to successfully implement it.

Consider the bottom left zone. In it, there is only one row where 9 can occur. Knowing that, I can cast it's "shadow" across the bottom middle zone.

The fourth strategy would be to look for the resulting square created by cells at row 3 column 4, row 3 column 6, row 7 column 4 and row 7 column 6. Knowing that 9 is a possibility in all four of these, we can deduce that It has to occur in both of these rows and columns. Aside from these cells, 9 can be eliminated as a possibility from the other cells in those rows and columns.

That makes it possible to deduce that the cell at row 4 column 5 has a value of 9. From that we can determine that row 5, column 2 has a value of 9. And from that we can figure out that row 8, column 3 has a value of 9.

Author:  rizzix [ Wed Nov 30, 2005 1:51 am ]
Post subject: 

Constraint Programming: Solving Sudoku (a recent article on java.net)

Author:  TheZsterBunny [ Tue Dec 13, 2005 8:03 pm ]
Post subject: 

just got around to posting now.
fixed up my algorithm a bit.

uh, its working in swing, which is nice, but its limited as of now to 9x9, possible puzzles (of _any_ difficulty)

here's the meat of the code, the rest is attached.
code:

        public void solve() {
                foundSolution = false;
                findSolution((byte) 0, grid);
        }

        private void findSolution(byte b, byte[][] param0) {   
                if (b == 81) {
                        foundSolution = true;
                        solvedGrid = param0;
                        return;
                }
                byte[][] temp= new byte [9][9];
                for (byte i = 0; i < 9; i++) {
                        for (byte j = 0; j < 9; j++) {
                                temp[i][j] = param0[i][j];
                        }
                }
               
                for (Byte t : getPossibleValues(param0,(byte) (b / 9), (byte) (b % 9))) {
                        if (!foundSolution) {
                                temp[b / 9][b % 9] = t;
                                findSolution((byte)(b+1), temp);
                        } else {
                                return;
                        }
                }
        }

        private TreeSet<Byte> getPossibleValues(byte [] [] param0, byte x, byte y) {
                TreeSet<Byte> returnable = new TreeSet<Byte>();
                if (gridChk[x][y] != CELL_LOCKED) {
                        for (byte i = 0; i <= 9; i++) {
                                returnable.add(i);
                        }
                        for (byte i = 0; i < 9; i++) {
                                returnable.remove(param0[x][i]);
                                returnable.remove(param0[i][y]);
                                returnable.remove(param0[((x / 3) * 3) + (i / 3)][((y / 3) * 3)
                                                + (i % 3)]);
                        }
                } else {
                        returnable.add(param0[x][y]);
                }
                return returnable;
        }

-Z

Author:  MysticVegeta [ Wed Dec 14, 2005 6:57 pm ]
Post subject: 

Hmm. I got my Turing solution to work but takes long time.

Strategy I used:

Plug in Random numbers for each cell..
Test if works.

Of course the random number I plug into the cell won't be in the row/column/3*3 grid.

Author:  MysticVegeta [ Sat Dec 24, 2005 10:48 pm ]
Post subject: 

Sorry I cannot see the button.. so anyways, heres my turing code, takes about 5-10 minutes to complete the solution... but hey! Its turing. There are several commented attempts/techniques I used but Random # brute force seems to be the best one that was able to solve the problem. Anyways, here.

code:
setscreen ("text")

var fi, cl : int
var mString : string
var grid, gCopy : array 1 .. 9, 1 .. 9 of string
var row, col : array 1 .. 2 of int
var bArray : array 1 .. 9 of boolean
var rowToStrArray : array 1 .. 9 of string
var rowLocations : flexible array 1 .. 0 of int
var randNum : int
var randNumString : string := ""
var counter := 1
var noneLeft : boolean := false

open : fi, "data.txt", get

%%%-----------------------------------------------------------------------------------------------%%%

%%% Boolean array initialization

for x : 1 .. 9
    get : fi, mString
    for y : 1 .. 9
        grid (x, y) := mString (y)
        gCopy (x, y) := grid (x, y)
    end for
end for

%%% Row and column storage used for random solution
% proc rowAndColStorage (r : int, c : int)
%     for x : 1 .. 9
%         bArray (x) := true
%     end for
%     for x : 1 .. 9
%         if (grid (r, x) not= "*") then
%             bArray (strint (grid (r, x))) := false
%         end if
%         if (grid (x, c) not= "*") then
%             bArray (strint (grid (x, c))) := false
%         end if
%     end for
% end rowAndColStorage
%
% %%% Three by three storage used for random solutiona
% proc threeByThree (currentR : int, currentC : int)
%     for r : row (1) .. row (2)
%         for c : col (1) .. col (2)
%             if grid (r, c) not= "*" then
%                 bArray (strint (grid (r, c))) := false
%             end if
%         end for
%     end for
% end threeByThree

%%%-----------------------------------------------------------------------------------------------%%%

%%%-----------------------------------------------------------------------------------------------%%%

%%% Normal method functions used for normal method solutions

%%% init the grid to g2
proc inti
    for x : 1 .. 9
        for y : 1 .. 9
            grid (x, y) := gCopy (x, y)
        end for
    end for
end inti

fcn checker : boolean
    for x : 1 .. 9
        for y : 1 .. 9
            if grid (x, y) = "*" then
                result true
            end if
        end for
    end for
    result false
end checker

%%% Rows and columns checker
fcn rowAndColChecker (num : string, r : int, c : int) : boolean
    for x : 1 .. 9
        if (grid (r, x) = num) or (grid (x, c) = num) then
            result false
        end if
    end for
    result true
end rowAndColChecker

%%% Check Three By Three
fcn threeBy3Checker (num : string, r : int, c : int) : boolean
    var x1, y1, x2, y2 : int
    if r <= 3 and c <= 3 then
        x1 := 1
        x2 := 3
        y1 := 1
        y2 := 3
    elsif r <= 3 and (c <= 6 and c >= 4) then
        x1 := 1
        x2 := 3
        y1 := 4
        y2 := 6
    elsif r <= 3 and (c <= 9 and c >= 7) then
        x1 := 1
        x2 := 3
        y1 := 7
        y2 := 9
    elsif (r <= 6 and r >= 4) and (c <= 3) then
        x1 := 4
        x2 := 6
        y1 := 1
        y2 := 3
    elsif (r <= 6 and r >= 4) and (c <= 6 and c >= 4) then
        x1 := 4
        x2 := 6
        y1 := 4
        y2 := 6
    elsif (r <= 6 and r >= 4) and (c <= 9 and c >= 7) then
        x1 := 4
        x2 := 6
        y1 := 7
        y2 := 9
    elsif (r <= 9 and r >= 7) and (c <= 3) then
        x1 := 7
        x2 := 9
        y1 := 1
        y2 := 3
    elsif (r <= 9 and r >= 7) and (c <= 6 and c >= 4) then
        x1 := 7
        x2 := 9
        y1 := 4
        y2 := 6
    elsif (r <= 9 and r >= 7) and (c <= 9 and c >= 7) then
        x1 := 7
        x2 := 9
        y1 := 7
        y2 := 9
    end if
    for x : x1 .. x2
        for y : y1 .. y2
            if grid (x, y) = num then
                result false
            end if
        end for
    end for
    result true
end threeBy3Checker

%%%-----------------------------------------------------------------------------------------------%%%

%%% Main Loop Processing

row (1) := 1
row (2) := 3
col (1) := 1
col (2) := 3

% for r : 1 .. 9
%     for c : 1 .. 9
%         if c > col (2) then
%             col (1) := c
%             col (2) := c + 2
%         end if
%         if grid (r, c) = "*" then
%             %%% NORMAL OLD METHOD
%
%             % for count : 1 .. 9
%             %     if ((rowAndColChecker (intstr (count), r, c)) and (threeBy3Checker (intstr (count), r, c))) then
%             %         grid (r, c) := intstr (count)
%             %         exit
%             %     end if
%             % end for
%
%             %%% RANDOM METHOD BEGINS
%
%             % rowAndColStorage (r, c)
%             % threeByThree (r, c)
%             % loop
%             %     randint (randNum, 1, 9)
%             %     if bArray (randNum) = true then
%             %         grid (r, c) := intstr (randNum)
%             %         for x : 1 .. 9
%             %             for y : 1 .. 9
%             %                 if y = 1 then
%             %                     put ""
%             %                 end if
%             %                 put grid (x, y), "-" ..
%             %             end for
%             %         end for
%             %         put ""
%             %         for x : 1 .. 9
%             %             put x, " ", bArray (x)
%             %         end for
%             %         Input.Pause
%             %         exit
%             %     end if
%             % end loop
%         end if
%         % if grid (r, c) = "*" then
%         %     put r, " ", c
%         % end if
%     end for
%     col (1) := 1
%     col (2) := 3
%     if r > row (2) then
%         row (1) := r
%         row (2) := r + 2
%     end if
% end

%%--------------------------------------------------------------------------------------------

%%% Thrid method solution

%%% Row checker

% for x : 1 .. 9
%     rowToStrArray (x) := ""
% end for
%
% for x : 1 .. 9
%     for y : 1 .. 9
%         rowToStrArray (x) += grid (x, y)
%     end for
% end for
%
% proc rowChecker (num : string)
%     for x : 1 .. 9
%         if index (rowToStrArray (x), num) = 0 then
%             new rowLocations, upper (rowLocations) + 1
%             rowLocations (upper (rowLocations)) := x
%         end if
%     end for
% end rowChecker
%
% fcn colChecker (num : string, c : int) : boolean
%     for x : 1 .. 9
%         if grid (x, c) = num then
%             result true
%         end if
%     end for
%     result false
% end colChecker
%
% proc rowChecker2 (num : string, r : int)
%     for x : 1 .. 9
%         if grid (r, x) = "*" then
%             if not (colChecker (num, x)) then
%                 put "(" + intstr (r) + "," + intstr (x) + ")" + " "
%             end if
%         end if
%     end for
% end rowChecker2
% %%--------------------------------------------------------------------------------------------
%
% for x : 1 .. 9
%     put x, " "
%     free rowLocations
%     rowChecker (intstr (x))
%     for y : 1 .. upper (rowLocations)
%         rowChecker2 (intstr (x), rowLocations (y))
%     end for
%     put ""
% end for

%%--------------------------------------------------------------------------------------------

%Fourth Attempt solution. (Random brute force)

proc solve
    for x : 1 .. 9
        for y : 1 .. 9
            randNumString := ""
            if grid (x, y) = "*" then
                loop
                    randNum := Rand.Int (1, 9)
                    if index (randNumString, intstr (randNum)) <= 0 then
                        randNumString += intstr (randNum)
                    end if
                    if length (randNumString) = 9 then
                        noneLeft := true
                        exit
                    end if
                    exit when (rowAndColChecker (intstr (randNum), x, y) and threeBy3Checker (intstr (randNum), x, y))
                end loop
                if not (noneLeft) then
                    grid (x, y) := intstr (randNum)
                end if
                if noneLeft then
                    return
                end if
            end if
        end for
    end for
end solve

%Plotting the grid
proc display
    for x : 1 .. 9
        for y : 1 .. 9
            if y = 1 then
                put ""
            end if
            put grid (x, y), "-" ..
        end for
    end for
end display

solve

loop
    randNumString := ""
    exit when not (checker)
    if noneLeft then
        noneLeft := false
    end if
    inti
    solve
end loop

display
clock (cl)
put skip, "Time Taken : ", round (cl / 1000), " seconds."


Note: For the method #4 which is the working one, most of the declared vars are not required because they were used by other attempts so I decided to leave it in....

Author:  MysticVegeta [ Sun Dec 25, 2005 12:15 pm ]
Post subject: 

zylum wrote:
here's a turing solution:

code:
var initialPossible : array 1 .. 9, 1 .. 9, 1 .. 9 of boolean
var grid : array 1 .. 9, 1 .. 9 of int

proc loadGrid (fileName : string)

    var file : int
    var line : string

    open : file, fileName, get

    for row : 1 .. 9
        get : file, line
        for col : 1 .. 9
            if line (col) = "*" then
                grid (col, row) := -1
                for i : 1 .. 9
                    initialPossible (col, row, i) := true
                end for
            else
                grid (col, row) := strint (line (col))
                for i : 1 .. 9
                    initialPossible (col, row, i) := false
                end for
            end if
        end for
    end for

    close (file)

    for col : 1 .. 9
        for row : 1 .. 9
            if grid (col, row) < 0 then
                for i : 1 .. 9
                    if grid (i, row) > 0 then
                        initialPossible (col, row, grid (i, row)) := false
                    end if
                    if grid (col, i) > 0 then
                        initialPossible (col, row, grid (col, i)) := false
                    end if
                end for
                for i : 1 .. 3
                    for j : 1 .. 3
                        if grid (((col - 1) div 3) * 3 + i, ((row - 1) div 3) * 3 + j) > 0 then
                            initialPossible (col, row, grid (((col - 1) div 3) * 3 + i, ((row - 1) div 3) * 3 + j)) := false
                        end if
                    end for
                end for
            end if
        end for
    end for

end loadGrid

proc solve (possible : array 1 .. 9, 1 .. 9, 1 .. 9 of boolean, c, r : int)

    if r = 10 then
        for row : 1 .. 9
            for col : 1 .. 9
                put grid (col, row), " " ..
            end for
            put ""
        end for
        return
    end if

    if grid (c, r) < 0 then
        var tempPossible := possible

        for i : 1 .. 9
            if tempPossible (c, r, i) then
                grid (c, r) := i

                for j : 1 .. 9
                    tempPossible (j, r, grid (c, r)) := false
                    tempPossible (c, j, grid (c, r)) := false
                end for

                for j : 1 .. 3
                    for k : 1 .. 3
                        tempPossible (((c - 1) div 3) * 3 + j, ((r - 1) div 3) * 3 + k, grid (c, r)) := false
                    end for
                end for

                if c < 9 then
                    solve (tempPossible, c + 1, r)
                else
                    solve (tempPossible, 1, r + 1)
                end if

                tempPossible := possible

                grid (c, r) := -1
            end if
        end for
    else
        if c < 9 then
            solve (possible, c + 1, r)
        else
            solve (possible, 1, r + 1)
        end if
    end if

end solve

loadGrid ("grid2.txt")
solve (initialPossible, 1, 1)

put "\n\n", Time.Elapsed


have fun Wink


Still no edit button Sad Zylum, please take a minute to explain the confusing recursion you used here.. I am trying hard to understand it but getting nowehre. Thanks a lot, Its way better than the random plugin solution lol.

Author:  zylum [ Tue Dec 27, 2005 1:43 am ]
Post subject: 

its not that confusing... if the column is 10 that means i have filled in the last number and i print out the grid. otherwise i plug in one of the possiblilities for the current cell and then move on to the next cell... thats about it... along the way i update the possible numbers for the other cells to speed things up.

Author:  MysticVegeta [ Tue Dec 27, 2005 11:20 am ]
Post subject: 

zylum wrote:
along the way i update the possible numbers for the other cells to speed things up.


Ah thats the only part I am missing. and whats the logic behind updating the possible numbers? Because For example:

Cell 1: Possibilities : 2,4. Taken #: 2
Cell 2: Posssibilities: 5, 7. Taken #: 5
Cell 3 : Possibilities : none because of some error in cell 1 and cell 2.

So, what would you update because Either (2 is right and 5 is wrong) or (2 is wrong 5 is right) or (2 is wrong and 5 is wrong)

Author:  Bored [ Tue Jan 31, 2006 11:07 pm ]
Post subject: 

I've created a Turing sudoku solver that uses recursion to solve the puzzle. It can solve any sudoku you throw at it because it can potentially check every possible solution until it fins the right one. I've compiled it incase you don't have turing and I've also added the source code that even if you don't know turing you should be able to follow. It works fairly fast but some puzzles may take quite a few seconds to solve. It starts by having you input a puzzle using arrow keys to move along the grid, numbers to input values, 0 or delete to delete a value and enter to solve puzzle. Try whatever soduko you can find and just try to beat it (with a solvable suduko).

Author:  Bored [ Tue Jan 31, 2006 11:21 pm ]
Post subject: 

Fewww, Now that I look through I'm gald I didn't look past page 2 cus some people had already posted answers. Now that I look back I see the first post of a recursive solution. I had that same problem with mine where it would erase some of the clues. Turned out when it was reseting values back to zero whether they where a clue or not and I just had to move the 3 lines that reset the values back to 0.


: