From 7c60319059cf9426677f2525317cb6ca77b052e4 Mon Sep 17 00:00:00 2001 From: Polgár Márton Date: Tue, 21 Jun 2022 06:18:54 +0200 Subject: Weekly solutions by 2colours --- challenge-170/2colours/raku/ch-1.raku | 11 +++++++++ challenge-170/2colours/raku/ch-2.raku | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100755 challenge-170/2colours/raku/ch-1.raku create mode 100755 challenge-170/2colours/raku/ch-2.raku diff --git a/challenge-170/2colours/raku/ch-1.raku b/challenge-170/2colours/raku/ch-1.raku new file mode 100755 index 0000000000..ec64423905 --- /dev/null +++ b/challenge-170/2colours/raku/ch-1.raku @@ -0,0 +1,11 @@ +#!/usr/bin/env raku + +(1 .. *) + .grep: &is-prime andthen + [\*] $_ andthen + .head: 10 andthen + .pairs + .map: { "P({.key + 1}) = {.value}" } andthen + .join: "\n" andthen + .say; + diff --git a/challenge-170/2colours/raku/ch-2.raku b/challenge-170/2colours/raku/ch-2.raku new file mode 100755 index 0000000000..322415c159 --- /dev/null +++ b/challenge-170/2colours/raku/ch-2.raku @@ -0,0 +1,43 @@ +#!/usr/bin/env raku + +sub prompt-matrix($name) { + my $starting-prompt = "$name = "; + my $indent-prompt = ' ' x $starting-prompt.chars; + my @prompts = lazy flat $starting-prompt, $indent-prompt xx *; + gather for @prompts -> $prompt { + given prompt($prompt).comb(/ '-'? \d+ /)>>.Int { + last if .elems == 0; + .take; + } + } andthen .List; +} + +sub infix:«~>»(@a, @b) { + |@a, |@b +} + +sub infix:<⊗>($a, $b) { + my @by-matrix = @$a XX<<*<< $b; + [~>] @by-matrix.map({ [Z~>] $_ }) +} + +sub infix:«<_»($output, $size) { + sprintf('%*s', $size, $output) +} + +sub show-matrix($caption, $lines) { + my $starting-prefix = "$caption = "; + my $indent = ' ' x $starting-prefix.chars; + my @prefixes = lazy flat $starting-prefix, $indent xx *; + my @cols = [Zmax] $lines>>.chars; + dd @cols; + $lines >><_>> (@cols,) andthen + .map: { "[ {.join: ' '} ]" } andthen + @prefixes <<~<< $_ andthen + .join: "\n" andthen + .say; +} + +my $a = prompt-matrix('A'); +my $b = prompt-matrix('B'); +show-matrix 'A ⊗ B', $a ⊗ $b; \ No newline at end of file -- cgit