aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Ferrari <fluca1978@gmail.com>2022-04-25 19:23:30 +0200
committerLuca Ferrari <fluca1978@gmail.com>2022-04-25 19:23:30 +0200
commitbd74d28150c9aedc46984dd57d5daeef7349e174 (patch)
tree9559b590859a506ab3635ccae2310d030b38544e
parent5c942ec63b9877471e867eecf1261d0016bfe261 (diff)
downloadperlweeklychallenge-club-bd74d28150c9aedc46984dd57d5daeef7349e174.tar.gz
perlweeklychallenge-club-bd74d28150c9aedc46984dd57d5daeef7349e174.tar.bz2
perlweeklychallenge-club-bd74d28150c9aedc46984dd57d5daeef7349e174.zip
Partial solution 2
-rwxr-xr-xchallenge-162/luca-ferrari/raku/ch-2.p696
1 files changed, 96 insertions, 0 deletions
diff --git a/challenge-162/luca-ferrari/raku/ch-2.p6 b/challenge-162/luca-ferrari/raku/ch-2.p6
new file mode 100755
index 0000000000..d3e3e6e2fe
--- /dev/null
+++ b/challenge-162/luca-ferrari/raku/ch-2.p6
@@ -0,0 +1,96 @@
+#!raku
+
+sub build-grid( Str $passprhase ) {
+ my @grid;
+ my $counter = 0;
+ for $passprhase.comb {
+ # skip existing letters
+ next if @grid.grep( $_ );
+ next if $_ !~~ / <[ a .. z ]> /;
+
+ # insert this letter
+ @grid.push: $_;
+ @grid.push: '|' if ++$counter %% 5;
+ }
+
+ # add the other letters
+ unless ( @grid.grep( '|' ).elems == 5 ) {
+ for 'a' .. 'z' {
+ next if @grid.grep( $_ );
+ next if $_ ~~ / i /;
+ next if $_ ~~ / j /;
+
+ # insert this letter
+ @grid.push: $_;
+ @grid.push: '|' if ++$counter %% 5;
+ last if @grid.grep( '|' ).elems == 5;
+ }
+ }
+
+ # now split the array into a matrix
+ my @matrix;
+ for @grid.join.split( '|' ) {
+ next if ! $_;
+ @matrix.push: [ $_.comb ];
+ }
+
+ say @matrix;
+ return @matrix;
+}
+
+
+sub build-pairs( Str $needle ) {
+
+ my @mangled;
+ my @current;
+ for $needle.comb {
+ next if $_ ~~ / ' ' /;
+
+ @current.push: $_ if ! @mangled.grep( $_ );
+ @current.push: 'X' if @mangled.grep( $_ );
+
+ if @current.elems == 2 {
+ @mangled.push: [@current];
+ @current = ();
+ }
+ }
+
+ return @mangled;
+}
+
+
+sub MAIN() {
+ my @key-matrix = build-grid( 'playfair example' );
+ my @pairs = build-pairs( 'hide the gold in the tree stump' );
+
+
+ for @pairs -> @current-pair {
+ my @rectangle;
+
+ for 0 ..^ @current-pair {
+ my $current_letter = @current-pair[ $_ ];
+ my $found = False;
+
+ for 0 ..^ @key-matrix.elems -> $row {
+ for 0 ..^ 5 -> $column {
+ if @key-matrix[ $row ][ $column ] ~~ $current_letter {
+ # found!
+ @rectangle.push: ( row => $row, col => $column ).Hash;
+ $found = True;
+ }
+
+ last if $found;
+ }
+
+ last if $found;
+ }
+ }
+
+ say @current-pair.join;
+ say @rectangle;
+
+ say @rectangle[ 0 ].^name;
+# my $is-rectangle = @rectangle[ 0 ]{ row } != @rectangle[ 1 ]{ row } && @rectangle[ 0 ]{ col } != @rectangle[ 1 ]{ col };
+# say "rettangolo $is-rectangle";
+ }
+}