aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author冯昶 <seaker@qq.com>2020-11-10 15:02:59 +0800
committer冯昶 <seaker@qq.com>2020-11-10 15:02:59 +0800
commit4ef2e21ee33b4d55df57658847ec36c37573c032 (patch)
tree6d9c6f2ab7babe0a9b51c5c4ea606fe2021c204d
parent9bcf46c41267a1d531025b90ed535f23d272f345 (diff)
downloadperlweeklychallenge-club-4ef2e21ee33b4d55df57658847ec36c37573c032.tar.gz
perlweeklychallenge-club-4ef2e21ee33b4d55df57658847ec36c37573c032.tar.bz2
perlweeklychallenge-club-4ef2e21ee33b4d55df57658847ec36c37573c032.zip
code cleaning
-rwxr-xr-xchallenge-086/feng-chang/raku/ch-2.raku75
1 files changed, 19 insertions, 56 deletions
diff --git a/challenge-086/feng-chang/raku/ch-2.raku b/challenge-086/feng-chang/raku/ch-2.raku
index 055c597ea2..3abf043233 100755
--- a/challenge-086/feng-chang/raku/ch-2.raku
+++ b/challenge-086/feng-chang/raku/ch-2.raku
@@ -15,24 +15,23 @@ constant @cols-index =
[7,16,25,34,43,52,61,70,79],
[8,17,26,35,44,53,62,71,80];
constant @squares-index =
- [0,1,2, 9,10,11, 18,19,20],
- [3,4,5, 12,13,14, 21,22,23],
- [6,7,8, 15,16,17, 24,25,26],
- [27,28,29, 36,37,38, 45,46,47],
- [30,31,32, 39,40,41, 48,49,50],
- [33,34,35, 42,43,44, 51,52,53],
- [54,55,56, 63,64,65, 72,73,74],
- [57,58,59, 66,67,68, 75,76,77],
+ [0,1,2, 9,10,11, 18,19,20],
+ [3,4,5, 12,13,14, 21,22,23],
+ [6,7,8, 15,16,17, 24,25,26],
+ [27,28,29, 36,37,38, 45,46,47],
+ [30,31,32, 39,40,41, 48,49,50],
+ [33,34,35, 42,43,44, 51,52,53],
+ [54,55,56, 63,64,65, 72,73,74],
+ [57,58,59, 66,67,68, 75,76,77],
[60,61,62, 69,70,71, 78,79,80];
+my @is-uncertain;
-sub is-done(@cells where *.elems == 9 --> Bool) {
- @cells.sort eqv (1...9);
-}
+sub is-done(@a where *.elems == 9 --> Bool) { @a.sort eqv (1...9) }
sub is-complete(@sdk where *.elems == 81 --> Bool) {
- ([and] (0..8)».&{ is-done(@sdk[@rows-index[$_]]) }) and
- ([and] (0..8)».&{ is-done(@sdk[@cols-index[$_]]) }) and
- ([and] (0..8)».&{ is-done(@sdk[@squares-index[$_]]) });
+ ([and] (^9)».&{ is-done(@sdk[@rows-index[$_]]) }) and
+ ([and] (^9)».&{ is-done(@sdk[@cols-index[$_]]) }) and
+ ([and] (^9)».&{ is-done(@sdk[@squares-index[$_]]) });
}
sub is-wrong(@cells where *.elems == 9 --> Bool) {
@@ -41,21 +40,15 @@ sub is-wrong(@cells where *.elems == 9 --> Bool) {
}
sub contradict(@sdk where *.elems == 81 --> Bool) {
- ([or] (0..8)».&{ is-wrong(@sdk[@rows-index[$_]]) }) or
- ([or] (0..8)».&{ is-wrong(@sdk[@cols-index[$_]]) }) or
- ([or] (0..8)».&{ is-wrong(@sdk[@squares-index[$_]]) });
+ ([or] (^9)».&{ is-wrong(@sdk[@rows-index[$_]]) }) or
+ ([or] (^9)».&{ is-wrong(@sdk[@cols-index[$_]]) }) or
+ ([or] (^9)».&{ is-wrong(@sdk[@squares-index[$_]]) });
}
-sub show-solution(@sdk where *.elems == 81) {
- put '=' x 17;
- put @sdk[@rows-index[$_]] for 0..8;
-}
-
-my @is-uncertain;
-
sub solve(@sdk is copy, UInt:D $pos) {
if is-complete(@sdk) {
- show-solution(@sdk);
+ put '=' x 17;
+ put @sdk[@rows-index[$_]] for ^9;
return;
}
@@ -73,7 +66,7 @@ sub solve(@sdk is copy, UInt:D $pos) {
multi MAIN($data-file) {
my @puzzle = $data-file.IO.words;
- for 0..80 -> $i {
+ for ^81 -> $i {
if @puzzle[$i] eq '_' {
@puzzle[$i] = 0;
@is-uncertain[$i] = True;
@@ -85,33 +78,3 @@ multi MAIN($data-file) {
solve(@puzzle, 0);
}
-
-multi MAIN('test') {
- my @puzzle = 'data.txt'.IO.words;
- for 0..80 -> $i {
- if @puzzle[$i] eq '_' {
- @puzzle[$i] = 0;
- } else {
- @puzzle[$i] .= Int;
- }
- }
-
- use Test;
- plan 7;
-
- is contradict(@puzzle), False, 'the initial puzzle has no contradiction';
-
- @puzzle[0] = 1;
- is is-wrong([1, 0, 0, 2, 6, 0, 7, 0, 1]), True, "two 1's in a row";
- is is-wrong(@puzzle[@rows-index[0]]), True, 'row 1 has contradiction';
- is ([or] (0..8)».&{ is-wrong(@puzzle[@rows-index[$_]]) }), True, 'puzzle has contradiction';
- is contradict(@puzzle), True, '1 at up-left produces contradiction';
-
- @puzzle[0] = 2;
- is contradict(@puzzle), True, '2 at up-left produces contradiction';
-
- @puzzle[0] = 3;
- is contradict(@puzzle), False, '3 at up-left has no contradiction';
-
- done-testing;
-}