aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchirvasitua <stuart-little@users.noreply.github.com>2020-11-16 14:15:32 -0500
committerchirvasitua <stuart-little@users.noreply.github.com>2020-11-16 14:15:32 -0500
commitb53ba49668d617bd083e0a201d27fc0131fe10b2 (patch)
tree6cc2767d0528706c72036a8c4e475cfb42329de1
parentcff21bb0b22502e3b0ef8a4f3946e5f921647115 (diff)
downloadperlweeklychallenge-club-b53ba49668d617bd083e0a201d27fc0131fe10b2.tar.gz
perlweeklychallenge-club-b53ba49668d617bd083e0a201d27fc0131fe10b2.tar.bz2
perlweeklychallenge-club-b53ba49668d617bd083e0a201d27fc0131fe10b2.zip
initial commit on 054,055
-rw-r--r--challenge-054/stuart-little/README1
-rwxr-xr-xchallenge-054/stuart-little/raku/ch-1.p621
-rwxr-xr-xchallenge-054/stuart-little/raku/ch-2.p645
-rw-r--r--challenge-055/stuart-little/README1
-rwxr-xr-xchallenge-055/stuart-little/raku/ch-1.p610
-rwxr-xr-xchallenge-055/stuart-little/raku/ch-2.p613
6 files changed, 91 insertions, 0 deletions
diff --git a/challenge-054/stuart-little/README b/challenge-054/stuart-little/README
new file mode 100644
index 0000000000..78439907de
--- /dev/null
+++ b/challenge-054/stuart-little/README
@@ -0,0 +1 @@
+Solutions by Stuart Little
diff --git a/challenge-054/stuart-little/raku/ch-1.p6 b/challenge-054/stuart-little/raku/ch-1.p6
new file mode 100755
index 0000000000..3530a98037
--- /dev/null
+++ b/challenge-054/stuart-little/raku/ch-1.p6
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub rk_perm(@a, $k) {
+ given my ($ix,$fact) = ((1..@a.elems) Z [\*](1..@a.elems)).first({ $_[1] < $k }, :end) {
+ when ! *.[0].defined { @a }
+ when *.[0] == @a.elems { @a.reverse }
+ when *.[0] < @a.elems-1 {
+ (|@a[^(@a.elems-$ix-1)], |rk_perm(@a[(@a.elems-$ix-1)..*], $k))
+ }
+ default {
+ my $init_ix=ceiling($k/$fact)-1;
+ my @rest=(@a.keys (-) $init_ix).keys.sort;
+ (@a[$init_ix], |rk_perm(@a[@rest], $k-($fact * (ceiling($k/$fact)-1))))
+ }
+ }
+}
+
+say rk_perm((1..@*ARGS[0].Int).List, (@*ARGS[1] || 1).Int);
+
+# run as <script> <$n for which we're permuting 1..$n> <the rank $k of the permutation you want>
diff --git a/challenge-054/stuart-little/raku/ch-2.p6 b/challenge-054/stuart-little/raku/ch-2.p6
new file mode 100755
index 0000000000..7c71620fc1
--- /dev/null
+++ b/challenge-054/stuart-little/raku/ch-2.p6
@@ -0,0 +1,45 @@
+#!/usr/bin/env perl6
+use v6;
+
+my %*SUB-MAIN-OPTS=:named-anywhere,;
+my %cllens = (1 => 1);
+
+sub cltz($n) { ($n %% 2) ?? ($n div 2) !! (($n > 1) ?? (3*$n+1) !! (1)) }
+
+sub cllen($n) {
+ %cllens{$n} //= 1+cllen($n.&cltz)
+}
+
+sub cltz_seq(Int $n where * > 0) {
+ given $n {
+ when 1 { (1,) }
+ when * %% 2 { ($n,|cltz_seq($n div 2)) }
+ default { ($n,|cltz_seq(3*$n+1)) }
+ }
+}
+
+multi sub MAIN(
+ Bool :b(:$bonus),
+) {
+ my $L=1_000_000;
+ my $wndw=20;
+ my @maxn=[1];
+
+ for (1..$L) {
+ my $ln = cllen($_);
+ ($ln > %cllens{@maxn}.max) && do {
+ (@maxn.elems == $wndw) && @maxn.shift;
+ @maxn.push($_)
+ }
+ }
+
+ for @maxn {
+ say $_ ~ " " ~ %cllens{$_}
+ }
+}
+
+multi sub MAIN(
+ *@ARGS,
+) {
+ say @ARGS[0].Int.&cltz_seq;
+}
diff --git a/challenge-055/stuart-little/README b/challenge-055/stuart-little/README
new file mode 100644
index 0000000000..78439907de
--- /dev/null
+++ b/challenge-055/stuart-little/README
@@ -0,0 +1 @@
+Solutions by Stuart Little
diff --git a/challenge-055/stuart-little/raku/ch-1.p6 b/challenge-055/stuart-little/raku/ch-1.p6
new file mode 100755
index 0000000000..bd77d55d9e
--- /dev/null
+++ b/challenge-055/stuart-little/raku/ch-1.p6
@@ -0,0 +1,10 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub max1flip(Str $s where * ~~ /^(0|1)*$/) {
+ ((^$s.chars) X (^$s.chars)).grep({ $_[0] <= $_[1] }).classify({ $s.substr($_[0]..$_[1]).comb.grep(0).elems - $s.substr($_[0]..$_[1]).comb.grep(1).elems }).max(*.key).value
+}
+
+say @*ARGS[0].&max1flip
+
+# run as <script> <binary string>
diff --git a/challenge-055/stuart-little/raku/ch-2.p6 b/challenge-055/stuart-little/raku/ch-2.p6
new file mode 100755
index 0000000000..49aece53ba
--- /dev/null
+++ b/challenge-055/stuart-little/raku/ch-2.p6
@@ -0,0 +1,13 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub wv(@a,$b) {
+ $b eqv ().Bag && do {say @a; return};
+ for $b.keys.grep({ (@a %% 2) ?? ($_ >= (@a[*-1] || -Inf)) !! ($_ <= (@a[*-1] || Inf)) }) {
+ wv((|@a,$_),($b (-) $_))
+ }
+}
+
+wv((), @*ARGS.map(*.Int).Bag)
+
+# run as <script> <space-separated integers>