aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchirvasitua <stuart-little@users.noreply.github.com>2021-05-17 18:28:28 -0400
committerchirvasitua <stuart-little@users.noreply.github.com>2021-05-17 18:28:28 -0400
commit1a3f2b41e483f11242ce8ff89cc240dce671e305 (patch)
treecf0595027080bc2b32df47859f522661d360d976
parentc3cd45087006d3f63b05219b8280a25dc1ea7ba9 (diff)
downloadperlweeklychallenge-club-1a3f2b41e483f11242ce8ff89cc240dce671e305.tar.gz
perlweeklychallenge-club-1a3f2b41e483f11242ce8ff89cc240dce671e305.tar.bz2
perlweeklychallenge-club-1a3f2b41e483f11242ce8ff89cc240dce671e305.zip
1st commit on 113_raku
-rwxr-xr-xchallenge-113/stuart-little/raku/ch-1.p621
-rwxr-xr-xchallenge-113/stuart-little/raku/ch-2.p643
2 files changed, 64 insertions, 0 deletions
diff --git a/challenge-113/stuart-little/raku/ch-1.p6 b/challenge-113/stuart-little/raku/ch-1.p6
new file mode 100755
index 0000000000..2df0b1ccfb
--- /dev/null
+++ b/challenge-113/stuart-little/raku/ch-1.p6
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl6
+use v6;
+
+# run <script> <number> <digit>
+
+sub lastDigSumm($nr,$dig,$nrSummands) {
+ return (($nr - $nrSummands * $dig) %% 10) && ($nrSummands * $dig <= $nr) && ($nrSummands * (($dig -1) * 10 + $dig) >= $nr);
+}
+
+sub lastDig($nr,$dig) {
+ return (1..9).grep({ lastDigSumm($nr,$dig,$_) }).Bool;
+}
+
+sub sol($nr,$dig) {
+ $dig == 0 && return ($nr >= 101 || ($nr %% 10));
+ return (($nr >= $dig * 11) || lastDig($nr,$dig));
+}
+
+say sol(|@*ARGS.map(*.Int)).Int;
+
+
diff --git a/challenge-113/stuart-little/raku/ch-2.p6 b/challenge-113/stuart-little/raku/ch-2.p6
new file mode 100755
index 0000000000..81d47b2aaf
--- /dev/null
+++ b/challenge-113/stuart-little/raku/ch-2.p6
@@ -0,0 +1,43 @@
+#!/usr/bin/env perl6
+use v6;
+
+use MONKEY-SEE-NO-EVAL;
+
+sub diffIfNum($nr,$str) {
+ return ($str.Int.defined) ?? (($nr - $str.Int).Str) !! ($str);
+}
+
+sub list2lol(@l) {
+ (@l.elems == 0 || @l[0] eq '.') && return [""];
+ my $ix = @l[1..*].keys.first( -> $i { 2*@l[1..$i+1].grep({ $_ eq '.' }).elems > $i+1});
+ return [list2lol(@l[1..$ix+1]),list2lol(@l[$ix+2..*]),@l[0].Str];
+}
+
+my @INPUT = (@*ARGS.elems) ?? (@*ARGS) !! (<1 2 4 . 7 . . . 3 5 . . 6 . .>);
+my $sum = @INPUT.grep({ $_.Int.defined }).map(*.Int).sum;
+my @outTreeList = @INPUT.map({ diffIfNum($sum,$_) });
+
+my $str=list2lol(@outTreeList).raku;
+EVAL qq!
+use Tree::DAG_Node;
+my \$lol = $str;
+my \$tree=Tree::DAG_Node->lol_to_tree(\$lol);
+print map(\"\$_\\n\", \@\{\$tree->tree2string(\{ no_attributes => 1 \})\});
+!, :lang<Perl5>;
+
+=finish
+run <script> <tree in preorder form with '.' for empty nodes, entered as space-separated values>
+
+ref: https://stackoverflow.com/a/2676849/11064961
+
+e.g. 1 2 4 . 7 . . . 3 5 . . 6 . . represents the tree
+
+ 1
+ / \
+ 2 3
+ / / \
+ 4 5 6
+ \
+ 7
+
+given as an example in the problem formulation at https://perlweeklychallenge.org/blog/perl-weekly-challenge-113/#TASK2