aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Anderson <mark@andemark.io>2025-10-27 05:22:39 +0000
committerMark Anderson <mark@andemark.io>2025-10-27 05:22:39 +0000
commit6706a7a33b611995783292a1e5f677fa7a02aba7 (patch)
treec8339b46585b3391ba4113434aef630f8a5fdc4e
parent9b1eba54d5e59d05df44f336120a6a03be62a645 (diff)
downloadperlweeklychallenge-club-6706a7a33b611995783292a1e5f677fa7a02aba7.tar.gz
perlweeklychallenge-club-6706a7a33b611995783292a1e5f677fa7a02aba7.tar.bz2
perlweeklychallenge-club-6706a7a33b611995783292a1e5f677fa7a02aba7.zip
Challenge 345 Solutions (Raku)
-rw-r--r--challenge-345/mark-anderson/raku/ch-1.raku47
-rw-r--r--challenge-345/mark-anderson/raku/ch-2.raku55
2 files changed, 102 insertions, 0 deletions
diff --git a/challenge-345/mark-anderson/raku/ch-1.raku b/challenge-345/mark-anderson/raku/ch-1.raku
new file mode 100644
index 0000000000..8fba9f428c
--- /dev/null
+++ b/challenge-345/mark-anderson/raku/ch-1.raku
@@ -0,0 +1,47 @@
+#!/usr/bin/env raku
+use Test;
+
+is-deeply peak-positions([<1 3 2>]), (1,);
+is-deeply peak-positions([<2 4 6 5 3>]), (2,);
+is-deeply peak-positions([<1 2 3 2 4 1>]), (2,4);
+is-deeply peak-positions([<5 3 1>]), (0,);
+is-deeply peak-positions([<1 5 1 5 1 5 1>]), (1,3,5);
+
+my @arr = (^100).roll(1000);
+is-deeply peak-positions(@arr), peak-positions-alternate(@arr);
+
+sub peak-positions(@ints)
+{
+ @ints.unshift(0);
+ @ints.push(0);
+
+ @ints.pairs
+ .rotor(3 => -2)
+ .grep({ .[0].value < .[1].value > .[2].value })
+ .map( { .[0].key })
+}
+
+sub peak-positions-alternate(@ints)
+{
+ my @histogram;
+
+ @ints.kv.map(-> $k,$v { @histogram[$k] = flat 'x' xx $v,
+ ' ' xx @ints.max - $v });
+
+ @histogram = [Z] @histogram;
+ @histogram .= map(*.Array);
+
+ .sort given gather for @histogram -> @row
+ {
+ last if @row.none eq 'x';
+
+ if @row.join ~~ m:g/<< x >>/ -> @m
+ {
+ for @m
+ {
+ take .from - 1;
+ @histogram[*;.from] = ' ' xx *
+ }
+ }
+ }
+}
diff --git a/challenge-345/mark-anderson/raku/ch-2.raku b/challenge-345/mark-anderson/raku/ch-2.raku
new file mode 100644
index 0000000000..100e34e242
--- /dev/null
+++ b/challenge-345/mark-anderson/raku/ch-2.raku
@@ -0,0 +1,55 @@
+#!/usr/bin/env raku
+use v6.e.PREVIEW;
+use Test;
+
+is-deeply last-visitor(<5 -1 -1>), [<5 -1>];
+is-deeply last-visitor(<3 7 -1 -1 -1>), [<7 3 -1>];
+is-deeply last-visitor(<2 -1 4 -1 -1>), [<2 4 2>];
+is-deeply last-visitor(<10 20 -1 30 -1 -1>), [<20 30 20>];
+is-deeply last-visitor(<-1 -1 5 -1>), [<-1 -1 5>];
+
+my @arr = (-1 xx 1000, 1..1000).flat.roll(10_000);
+is last-visitor(@arr), last-visitor-alternate(@arr);
+
+sub last-visitor(@ints)
+{
+ my @ans;
+ my @seen;
+ my $seq = flat (@ints.head > 0 ?? (* > 0, * == -1) !! (* == -1, * > 0)) xx *;
+ my @snip = @ints.snip: $seq;
+
+ @ans.append: @snip.shift.flat if @snip.head.head == -1;
+ @snip.pop if @snip.tail.tail > 0;
+
+ for @snip -> @pos, @neg-ones
+ {
+ @seen.append: @pos;
+ @ans.append: flat (reverse @seen.tail: @neg-ones), @neg-ones.tail: @neg-ones - @seen
+ }
+
+ return @ans
+}
+
+sub last-visitor-alternate(@ints)
+{
+ my $x = 0;
+ my @seen;
+ my @ans;
+
+ for @ints -> $i
+ {
+ if $i.sign == 1
+ {
+ $x = 0;
+ @seen.unshift: $i
+ }
+
+ else
+ {
+ @ans.push: @seen[$x] // -1;
+ $x++
+ }
+ }
+
+ return @ans
+}