From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id CA042BB9A for ; Tue, 15 Nov 2005 10:00:52 +0100 (CET) Received: from smtp4-g19.free.fr (smtp4-g19.free.fr [212.27.42.30]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id jAF90qjE005818 for ; Tue, 15 Nov 2005 10:00:52 +0100 Received: from [192.168.0.2] (rke75-3-82-229-183-156.fbx.proxad.net [82.229.183.156]) by smtp4-g19.free.fr (Postfix) with ESMTP id 3D04940176; Tue, 15 Nov 2005 10:00:51 +0100 (CET) Message-ID: <4379A294.1050007@inria.fr> Date: Tue, 15 Nov 2005 09:55:48 +0100 From: Alain Frisch User-Agent: Debian Thunderbird 1.0.2 (X11/20050602) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Jon Harrop Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Sudoku solver References: <200511150427.45996.jon@ffconsultancy.com> In-Reply-To: <200511150427.45996.jon@ffconsultancy.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 4379A3C4.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; frisch:01 frisch:01 caml-list:01 solver:01 ocaml:01 solver:01 ocamlopt:01 integers:01 branching:01 98%:98 37%:98 wrote:01 eleves:01 eleves:01 dev:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 Jon Harrop wrote: > Here is a little OCaml program to solve Sudoku puzzles: > > http://www.ffconsultancy.com/free/sudoku/ > Funny. My father told me about the game last sunday and I was willing to write a solver too :-) Here it is: http://www.eleves.ens.fr/home/frisch/info/af-sudoku-brute.ml http://www.eleves.ens.fr/home/frisch/info/af-sudoku.ml It is faster than yours. E.g., when displaying all the solutions (there are 247 solutions for the example): $ cat puzzle 001005300050490000 000102064 000000750 600000001 035000000 060903000 000020090 003600100 $ time ./sudoku < puzzle > /dev/null 4.89s user 0.00s system 98% cpu 4.944 total $ time ./af-sudoku-brute < puzzle > /dev/null 0.02s user 0.00s system 30% cpu 0.068 total $ time ./af-sudoku < puzzle > /dev/null 0.03s user 0.00s system 37% cpu 0.074 total (all of them are compiled with ocamlopt without any special option) I guess your choice of a functional data structure explains the 100x slow-down... Hint: copying an array of 81 integers is fast. The -brute version is a simple-minded brute force search. There other one tries to use the constraint "each digit must appear in each bloc" (where a bloc is a line, a column, or a 3x3 sub-bloc) to place digits. It also chooses a cell with a minimal number of remaining choices when branching. Interestingly, disabling these optimizations does not seem to change the performance significantly. -- Alain