ecube has asked for the wisdom of the Perl Monks concerning the following question:
Edmund von der Burg (ecclestoad.co.uk)has written a simple sudoku solver. The solver processes one sudoku and then dies.
To be able to handle a list of sudokus as input I adhoc replaced the "die" command by "warn".
That had the effect that the execution time for the 5 sudokos of the example increased from 82 s (for die) to 717 s.
How come?use integer; my $start = time; while(<DATA>){ if(/#----END----#/){ my $duration = time - $start; die "Execution time: $duration s\n"; } @A = split //,; &R; } sub R { for $i ( 0 .. 80 ) { next if $A[$i]; my %t = map { $_/9==$i/9||$_%9==$i%9||$_/27==$i/27&&$_%9/3==$i%9/3?$A[$_]: +0=>1 } 0 .. 80; R($A[$i]=$_) for grep{!$t{$_}} 1 .. 9; return $A[$i] = 0; } $sum=0; for(@A){$sum+=$_} print" $sum\n"; warn @A; # Execution time = 717 s # die @A; # Execution time 31 + 31 + 14 + 1 + 5 = 82 s } __DATA__ 1204003003000100500060001007000900000406030000030020005000807000070000 +05000000098 0000000390000010050030508000080900060700020001004000000090800500200006 +00400700000 1203000043500001000040000000054002006000700000000080900031005000000090 +70000060008 0030000004000800360080001000400600730009000000000020050040700686000000 +00700600500 8000000000036000000700902000500070000000457000001000300010000680085000 +10090000400 #----END----# 1284653793742198569568371427651984232496735818135429675923867144879216 +35631754298 7518462398923714656432598712381975469745623181654389273196847525279136 +84486725193 1263957843598471628746219539854162376319728452475386917631845294182593 +76592763418 1234567894571892369683271542495618735769384128317426953142759686958143 +27782693541 8127536499436821756754912831542378963698457212871695345219743684385269 +17796318452
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Sudoku solver
by moritz (Cardinal) on Aug 11, 2012 at 10:08 UTC | |
by davido (Cardinal) on Aug 11, 2012 at 15:49 UTC | |
by Anonymous Monk on Aug 11, 2012 at 11:33 UTC | |
Re: Sudoku solver
by Anonymous Monk on Aug 11, 2012 at 09:54 UTC | |
Re: Sudoku solver
by sundialsvc4 (Abbot) on Aug 11, 2012 at 14:49 UTC |
Back to
Seekers of Perl Wisdom