|
|
Up |
|
|
  |
Author: Anton ErtlAnton Ertl Date: Dec 17, 2006 10:57
Some weeks ago, I mentioned that I was working on a Sudoku solver
program. It did not go as smooth as a would have liked, so I decided
to do much less than I had originally envisioned. Also, other things
intervened, so it took much longer than I had planned. Anyway, you
can find the program at
http://www.complang.tuwien.ac.at/forth/programs/sudoku3.fs
It relies on a number of Gforth words (see the comments at the start).
It is also bigger and slower than Robert Spykerman's program, and it
solves only simple puzzles at the moment. But I am not going to work
on it in the next time, so I am publishing it, so that you can learn
from it, maybe find out what mistakes I made, or maybe improve it.
I am not sure if this is a good basis for continuing, though: The
problems I had with it were that I did not find a good factoring for
dealing with rows, columns, and boxes uniformely; maybe this could be
done better using lookup tables. Also, I found it pretty hard to
debug this stuff; one improvement might be if I used variable indices
instead of variable addresses in most places.
|
| Show full article (1.29Kb) |
|
| | 17 Comments |
|
  |
Author: Andreas KochenburgerAndreas Kochenburger Date: Dec 18, 2006 09:39
>I am not sure if this is a good basis for continuing, though: The
>problems I had with it were that I did not find a good factoring for
>dealing with rows, columns, and boxes uniformely; maybe this could be
>done better using lookup tables.
Only a side remark:
I found the Prolog code snippet below very interesting. It is so very
compact (cut away the tests) and - at least for me - an example of how
important the selection of the right tool is - here Prolog. If I had
free time to kill I'd perhaps try to mimick that in Joy or Forth. But
... :-(
Prolog code:
%%TO convert to gnuprolog remove the following two lines.
:- use_module(library(clpfd)).
:- use_module(library(lists)).
%%then search and replace all_different to fd_all_different
%%It should all work.
|
| Show full article (5.63Kb) |
|
| | 8 Comments |
|
  |
Author: Markus TriskaMarkus Triska Date: Dec 19, 2006 19:03
Andreas Kochenburger nospam.com> wrote:
> I found the Prolog code snippet below very interesting. It is so
> very compact (cut away the tests) and - at least for me - an example
> of how important the selection of the right tool is - here Prolog.
Here's a more concise version:
:- use_module(library(bounds)).
suudoku(P) :-
Rows = [R1,R2,R3,R4,R5,R6,R7,R8,R9],
problem(P, Rows),
flatten(Rows, Vars),
Vars in 1..9,
row_constraint(Rows),
column_constraint(R1, R2, R3, R4, R5, R6, R7, R8, R9),
block_constraint(R1, R2, R3),
block_constraint(R4, R5, R6),
block_constraint(R7, R8, R9),
once(label(Vars)),
display_rows(Rows).
|
| Show full article (2.34Kb) |
| 3 Comments |
|
  |
Author: Doug HoffmanDoug Hoffman Date: Dec 20, 2006 09:52
Markus Triska wrote:
> Here's a more concise version:
[snip]
Prolog looks pretty impressive. Thanks for the example.
Just to show that Forth can deal concisely with problems at a higher
level, I include the following example of a "List of Lists", analagous
to what you recently coded in the Prolog newsgroup. (I have no idea
how to approach the Sudoku problem. I have never solved one and don't
even know what the object of the game is):
*** BEGIN PROLOG POST
I need to create a list of words (divided by spaces) each one is a list
of chars.
Example:
|
| Show full article (2.43Kb) |
| 3 Comments |
|
  |
Author: Krishna MyneniKrishna Myneni Date: Dec 21, 2006 20:19
Doug Hoffman wrote:
> ...
> Prolog looks pretty impressive. Thanks for the example.
>
> Just to show that Forth can deal concisely with problems at a higher
> level, I include the following example of a "List of Lists"...
|
| Show full article (2.03Kb) |
| 2 Comments |
|
  |
Author: Jorge Acereda MaciáJorge Acereda Maciá Date: Dec 20, 2006 15:14
Not as nice or as fast as the Prolog solution, but here is my
brute-force approach:
: cell- 1 cells - ;
0 value start
0 value end
create colbuf 9 cells allot
create rowbuf 9 cells allot
create blkbuf 9 cells allot
: wipe ( addr -- , erase buffer)
9 cells erase ;
: row ( index -- row...
|
| Show full article (5.07Kb) |
| 2 Comments |
|
  |
Author: Doug HoffmanDoug Hoffman Date: Dec 22, 2006 04:17
Krishna Myneni wrote:
> It's important to use the right tools for conciseness. If one is to deal with
> lists of lists, IMHO the natural choice is a Lisp-like lists package in Forth.
> Using lists.4th, the code is pretty easy (sample run and code below):
I agree. I wasn't aware of the list code. It certainly fits this
problem very well! My approach was a bit more "build-your-own"
starting with a few pre-defined object classes.
Thanks for the example.
|
| |
| 1 Comment |
|
  |
Author: Krishna MyneniKrishna Myneni Date: Dec 22, 2006 05:38
Doug Hoffman wrote:
> Krishna Myneni wrote:
>
>
>>It's important to use the right tools for conciseness. If one is to deal with
>>lists of lists, IMHO the natural choice is a Lisp-like lists package in Forth.
>>Using lists.4th, the code is pretty easy (sample run and code below):
>
>
> I agree. I wasn't aware of the list code. It certainly fits this
> problem very well! My approach was a bit more "build-your-own"
> starting with a few pre-defined object classes.
>
> Thanks for the example.
>
I forgot to mention that the lists package has been tested under gforth as well.
See the files lists.fs, lists-test.fs, and strings.fs in the ftp directory given
below. The file gps.fs is another example of use of the lists package.
ftp://ccreweb.org/software/gforth/
|
| Show full article (0.79Kb) |
| no comments |
|
  |
Author: GerryGerry Date: Dec 30, 2006 07:34
From: "Gerry" jackson9000.fsnet.co.uk>
Newsgroups: comp.lang.forth
Subject: Re: Sudoku3
Date: Sat, 30 Dec 2006 07:27:08 -0800
Anton Ertl wrote:
[snip]
>> Certainly Prolog, especially with finite domain constraints, is a very
> good language for solving Sudoku and other puzzles. However, we
> already had a nice fast solver in Forth by Robert Spykerman, so the
> aim of my project was different: I wanted to be able to evaluate the
> difficulty of a puzzle, and eventually to generate puzzles of a given
> difficulty. The Prolog program you posted can do nothing of that.
>
[snip]
I doubt if this approach is what you were looking for but it gives some
idea of how the difficulty of a given puzzle could be assessed. I wrote
a Sudoku solver over a year ago and it followed these steps:
|
| Show full article (2.51Kb) |
| 3 Comments |
|
  |
|
|
  |
Author: Bernd PaysanBernd Paysan Date: Jan 1, 2007 12:18
> There are other methods of solving than brute force steps. I
> am going from memory of what I have read as I do not do sudoko
> myself. For example if you can find pairs, places where only
> two numbers are possible in a row etc or triplets you can use
> these to find more single solutions.
I got a Sudoku computer when I stated a c't subscription a month ago (due to
price raise for non-subscription buyers). So I've had now some time to do
sudokus by hand. The computer has three levels (easy/medium/hard), where I
can see little difference. The grade of the level is the number of free
choices you can do, i.e. on easy levels, there's maybe one choice and all
the other numbers fall into places, while on the hard level, there might be
four or five choices.
Since I do this manually, I don't do backtracking, but I rearrange the
numbers that don't fit when I made the wrong choice. This doesn't always
work out (i.e. I sometimes get stuck with the last few numbers, and can't
find a way to shuffle the rest around to solve the sudoku).
|
| Show full article (1.18Kb) |
| no comments |
|
|
|
|