From 1394f0691dd45f342843e5f507a1d25f47a974c0 Mon Sep 17 00:00:00 2001 From: juliodcs Date: Thu, 26 Nov 2020 19:23:17 +0100 Subject: small simplification --- challenge-086/juliodcs/perl/ch-2.pl | 28 +++++++--------------------- challenge-086/juliodcs/raku/ch-2.raku | 2 +- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/challenge-086/juliodcs/perl/ch-2.pl b/challenge-086/juliodcs/perl/ch-2.pl index 174e7bcc88..f1b2bfc62b 100644 --- a/challenge-086/juliodcs/perl/ch-2.pl +++ b/challenge-086/juliodcs/perl/ch-2.pl @@ -11,29 +11,15 @@ sub sudoku_solver($sudoku) { my $index = 0; while ($index < @indexes) { die 'No solution possible' if $index < 0; - + my ($y, $x) = get_position($indexes[$index]); - my $guess = make_guess($sudoku, $y, $x, $backtrack); - - if ($guess == 0 && !$backtrack) { - # not valid guess, We will try with next guess for same index - $backtrack = 1; - } - elsif ($guess == 0 && $backtrack) { - # not valid guess and no more guesses to test - # We give up with this position (we decrement index) - - $sudoku->[$y][$x] = 0; - $index--; - } - else { - # valid guess (for now), increase index - - $sudoku->[$y][$x] = $guess; - $backtrack = 0; - $index++; - } + + # If there's a guess, We set it and increase the index + # if there's no guess, We clean the box and decrement the index + $sudoku->[$y][$x] = $guess; + $backtrack = !$guess; + $index += $guess ? 1 : -1; } return $sudoku; diff --git a/challenge-086/juliodcs/raku/ch-2.raku b/challenge-086/juliodcs/raku/ch-2.raku index 44ac907224..1f3d304bf6 100644 --- a/challenge-086/juliodcs/raku/ch-2.raku +++ b/challenge-086/juliodcs/raku/ch-2.raku @@ -13,7 +13,7 @@ sub solve-sudoku(@sudoku) { # If there's a guess, We set it and increase the index # if there's no guess, We clean the box and decrement the index - @sudoku[$y;$x] = $guess ?? $guess !! 0; + @sudoku[$y;$x] = $guess; $backtrack = not so $guess; $index += $guess ?? 1 !! -1; } -- cgit