ADFGVX cipher: Difference between revisions
→{{header|Raku}}: Add a Raku example
(Added Go) |
Thundergnat (talk | contribs) (→{{header|Raku}}: Add a Raku example) |
||
Line 434:
<!--</lang>-->
Output matches Wren/Nim/wp when the appropriate lines are uncommented.
=={{header|Raku}}==
Slightly different results from every other entry so far. See discussion page for reasons.
<lang perl6>srand 123456; # For repeatability
my @header = < A D F G V X >;
my $polybius = (flat 'A'..'Z', 0..9).pick(*).join;
my $key-length = 9;
my $key = uc 'unixdict.txt'.IO.words.grep( { (.chars == $key-length) && (+.comb.Set == +.comb) } ).roll;
my %cypher = (@header X~ @header) Z=> $polybius.comb;
my $message = 'Attack at 1200AM';
use Terminal::Boxer;
say "Key: $key\n";
say "Polybius square:\n", ss-box :7col, :3cw, :indent("\t"), '', |@header, |(@header Z $polybius.comb.batch: 6);
say "Message to encode: $message";
say "\nEncoded: " ~ my $encoded = encode $message;
say "\nDecoded: " ~ decode $encoded;
sub encode ($text is copy) {
$text = $text.uc.comb(/<[A..Z 0..9]>/).join;
my @order = $key.comb.pairs.sort( *.value )».key;
my @encode = %cypher.invert.hash{ $text.comb }.join.comb.batch($key-length).map: { [$_] };
((^$key-length).map: { @encode».[@order]».grep( *.defined )».[$_].grep( *.defined ).join }).Str;
}
sub decode ($text is copy) {
my @text = $text.split(' ')».comb;
my $chars = @text[0].chars;
$_ = flat |$_, ' ' if .chars < $chars for @text;
my @order = $key.comb.pairs.sort( *.value )».key.pairs.sort( *.value )».key;
%cypher{ ( grep { /\w/ }, flat [Z] @order.map( { |@text.batch($key-length)».[$_] } ) ).batch(2)».join }.join;
}</lang>
{{out}}
<pre style="line-height:1.2em;">Key: GHOSTLIKE
Polybius square:
┌───┬───┬───┬───┬───┬───┬───┐
│ │ A │ D │ F │ G │ V │ X │
├───┼───┼───┼───┼───┼───┼───┤
│ A │ H │ O │ S │ 5 │ 7 │ Q │
├───┼───┼───┼───┼───┼───┼───┤
│ D │ 0 │ I │ 6 │ J │ C │ V │
├───┼───┼───┼───┼───┼───┼───┤
│ F │ 4 │ 8 │ R │ X │ G │ A │
├───┼───┼───┼───┼───┼───┼───┤
│ G │ Y │ F │ P │ B │ Z │ 3 │
├───┼───┼───┼───┼───┼───┼───┤
│ V │ M │ W │ 9 │ D │ 1 │ N │
├───┼───┼───┼───┼───┼───┼───┤
│ X │ 2 │ E │ U │ T │ L │ K │
└───┴───┴───┴───┴───┴───┴───┘
Message to encode: Attack at 1200AM
Encoded: DVVA FVX XXA FGF XVX GXA XXD GFA XXD
Decoded: ATTACKAT1200AM</pre>
=={{header|Wren}}==
|