Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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.

Replies are listed 'Best First'.
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.


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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://986885]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2018-06-18 18:07 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (110 votes). Check out past polls.