aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Mita <mienaikage@gmail.com>2019-11-19 13:59:14 +0000
committerDaniel Mita <mienaikage@gmail.com>2019-11-19 22:14:10 +0000
commit465b20ee77fef30d21b4af76553996bc3de2b720 (patch)
treeda0aba6361d24a896070782d856f0ab0b11de059
parent4b5f8423cee1b75c862ce9c50cc22da4196432c4 (diff)
downloadperlweeklychallenge-club-465b20ee77fef30d21b4af76553996bc3de2b720.tar.gz
perlweeklychallenge-club-465b20ee77fef30d21b4af76553996bc3de2b720.tar.bz2
perlweeklychallenge-club-465b20ee77fef30d21b4af76553996bc3de2b720.zip
Add Raku solutions for challenge-035
-rw-r--r--challenge-035/daniel-mita/perl6/Morse.pm692
-rwxr-xr-xchallenge-035/daniel-mita/perl6/ch-1.p619
-rwxr-xr-xchallenge-035/daniel-mita/perl6/ch-2.p619
3 files changed, 130 insertions, 0 deletions
diff --git a/challenge-035/daniel-mita/perl6/Morse.pm6 b/challenge-035/daniel-mita/perl6/Morse.pm6
new file mode 100644
index 0000000000..375fd03955
--- /dev/null
+++ b/challenge-035/daniel-mita/perl6/Morse.pm6
@@ -0,0 +1,92 @@
+my role X::Morse is Exception {
+ has $.payload;
+ method message { 'Morse error' }
+}
+
+my class X::Morse::InvalidCharacter does X::Morse {
+ method message { "Could not parse character: $!payload" }
+}
+
+module Morse {
+ my constant %values = (
+ A => 0b10111,
+ B => 0b111010101,
+ C => 0b11101011101,
+ D => 0b1110101,
+ E => 0b1,
+ F => 0b101011101,
+ G => 0b111011101,
+ H => 0b1010101,
+ I => 0b101,
+ J => 0b1011101110111,
+ K => 0b111010111,
+ L => 0b101110101,
+ M => 0b1110111,
+ N => 0b11101,
+ O => 0b11101110111,
+ P => 0b10111011101,
+ Q => 0b1110111010111,
+ R => 0b1011101,
+ S => 0b10101,
+ T => 0b111,
+ U => 0b1010111,
+ V => 0b101010111,
+ W => 0b101110111,
+ X => 0b11101010111,
+ Y => 0b1110101110111,
+ Z => 0b11101110101,
+ 1 => 0b10111011101110111,
+ 2 => 0b101011101110111,
+ 3 => 0b1010101110111,
+ 4 => 0b10101010111,
+ 5 => 0b101010101,
+ 6 => 0b11101010101,
+ 7 => 0b1110111010101,
+ 8 => 0b111011101110101,
+ 9 => 0b11101110111011101,
+ 0 => 0b1110111011101110111,
+ # TODO: punctuation
+ );
+ my constant %chars = %values.antipairs;
+
+ grammar Decode {
+ token TOP { <word>+ % '0' ** 7 }
+ token word { <char>+ % '000' }
+ token char { <mark>+ % '0' }
+ proto token mark {*}
+ token mark:sym<dot> { '1' }
+ token mark:sym<dash> { '111' }
+ }
+
+ class Decoder {
+ method TOP ($/) {
+ make $<word>.map(*.made).join(' ');
+ }
+
+ method word ($/) {
+ make $<char>.map(*.made).join;
+ }
+
+ method char ($/) {
+ make %chars{ $/.Str.parse-base(2) }
+ or X::Morse::InvalidCharacter.new( :payload($/) ).throw;
+ }
+ }
+
+ #| Decode binary morse code
+ sub decode (
+ Str $bits where *.comb.all eq 1|0, #= A string containing a sequence of 1s and 0s
+ --> Str
+ ) is export {
+ return Decode.parse( $bits, :actions(Decoder.new) ).made or
+ X::Morse.new.throw;
+ }
+
+ #| Encode binary morse code
+ sub encode (
+ Str $phrase,
+ --> Str
+ ) is export {
+ X::NYI.new( :feature(&?ROUTINE.name) ).throw;
+ }
+}
diff --git a/challenge-035/daniel-mita/perl6/ch-1.p6 b/challenge-035/daniel-mita/perl6/ch-1.p6
new file mode 100755
index 0000000000..9b2a502a05
--- /dev/null
+++ b/challenge-035/daniel-mita/perl6/ch-1.p6
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl6
+use v6.d;
+use lib $?FILE.IO.dirname;
+use Morse;
+
+# My intention to take &encode directly from Morse and
+# use it as MAIN here. This does what I intended, but
+# I'm unsure if it was the best way to go about it.
+
+sub MAIN (
+ |c where * ~~ &encode.signature,
+ --> Nil
+) {
+ encode(||c).say;
+}
+
+sub GENERATE-USAGE(|) {
+ RUN-MAIN(&encode, Nil);
+}
diff --git a/challenge-035/daniel-mita/perl6/ch-2.p6 b/challenge-035/daniel-mita/perl6/ch-2.p6
new file mode 100755
index 0000000000..7a72e65f2b
--- /dev/null
+++ b/challenge-035/daniel-mita/perl6/ch-2.p6
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl6
+use v6.d;
+use lib $?FILE.IO.dirname;
+use Morse;
+
+# My intention to take &decode directly from Morse and
+# use it as MAIN here. This does what I intended, but
+# I'm unsure if it was the best way to go about it.
+
+sub MAIN (
+ |c where * ~~ &decode.signature,
+ --> Nil
+) {
+ decode(||c).say;
+}
+
+sub GENERATE-USAGE(|) {
+ RUN-MAIN(&decode, Nil);
+}