Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Sudoku solver

by moritz (Cardinal)
on Aug 11, 2012 at 10:08 UTC ( #986885=note: print w/ replies, xml ) Need Help??


in reply to Sudoku solver

The die is used for control flow here -- there's a deep recursion going on, and once a solution is found, all of these recursive subs can be terminated at once. That's what die does.

And warn doesn't -- it just prints a warning, and doesn't abort the current routines. So it makes the solver search for more solutions even after a solution is found. Which is why it takes much longer.

A possible (still hacky) approach is to do the BLOCK form of eval to catch the exception, print the result and then start the recursion anew with the next Sudoku.


Comment on Re: Sudoku solver
Download Code
Re^2: Sudoku solver
by Anonymous Monk on Aug 11, 2012 at 11:33 UTC

    maybe like

    print forgetR( ... ); sub forgetR { local @A; if( 1 == @_ ){ @A = split //, @_; } else { @A = @_; } eval { R(); }; warn "CAUGHT DEATH $@ "; return @A; }

    forgetR takes either a line (like from __DATA__) or a list

Re^2: Sudoku solver
by davido (Archbishop) on Aug 11, 2012 at 15:49 UTC

    I actually found a non-obfuscated, non-golfed Sudoku solver in C++ once that used a similar technique; recurse, and then throw an exception for any branch that didn't pan out. There were exceptions at multiple levels, each being caught to wipe out branches. Worked pretty well, though I could faintly hear Dijkstra moaning when I ran it.

    I wish I could find and link to it now. It was interesting.


    Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://986885]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (12)
As of 2014-12-22 07:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (112 votes), past polls