aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark <53903062+andemark@users.noreply.github.com>2023-02-05 18:40:03 +0000
committerMark <53903062+andemark@users.noreply.github.com>2023-02-05 18:40:03 +0000
commit460fc0876009d5d6d1ca3a9ff59ff2ab43a1102e (patch)
tree46862d88f8d29927d24ad5a79632ed24e6ce8089
parenta91c92d5683d263845ed2ae08af470a276535ca4 (diff)
downloadperlweeklychallenge-club-460fc0876009d5d6d1ca3a9ff59ff2ab43a1102e.tar.gz
perlweeklychallenge-club-460fc0876009d5d6d1ca3a9ff59ff2ab43a1102e.tar.bz2
perlweeklychallenge-club-460fc0876009d5d6d1ca3a9ff59ff2ab43a1102e.zip
ch-2.raku do-over
-rw-r--r--challenge-202/mark-anderson/raku/ch-2.raku49
1 files changed, 25 insertions, 24 deletions
diff --git a/challenge-202/mark-anderson/raku/ch-2.raku b/challenge-202/mark-anderson/raku/ch-2.raku
index d29886a5f8..ef80125df6 100644
--- a/challenge-202/mark-anderson/raku/ch-2.raku
+++ b/challenge-202/mark-anderson/raku/ch-2.raku
@@ -1,33 +1,34 @@
#!/usr/bin/env raku
+use Test;
-say widest-valley(< 1 2 2 5 4 1 0 2 3 2 1 3 4 3 1 32 11 11 8 >);
-say widest-valley(< 1 2 2 5 4 1 0 0 0 0 0 2 3 4 5 32 11 11 8 >);
-say widest-valley(< 1 1 1 1 1 2 5 >);
-say widest-valley(< 32 11 11 8 >);
-say widest-valley(< 11 11 11 8 4 2 4 8 11 11 11 >);
-say widest-valley((10..50).roll(1000));
+is-deeply widest-valley(1, 5, 5, 2, 8),
+ (5, 5, 2, 8), 'Example 1';
+is-deeply widest-valley(2, 6, 8, 5),
+ (2, 6, 8), 'Example 2';
+is-deeply widest-valley(9, 8, 13, 13, 2, 2, 15, 17),
+ (13, 13, 2, 2, 15, 17), 'Example 3';
+is-deeply widest-valley(1, 3, 3, 2, 1, 2, 3, 3, 2),
+ (3, 3, 2, 1, 2, 3, 3), 'Example 4';
+is-deeply widest-valley(2, 1, 2, 1, 3),
+ (2, 1, 2), 'Example 5';
-sub widest-valley(*@a)
-{
- my @decreasing;
- my @increasing;
+say widest-valley((10..50).roll(100_000)); # takes ~ 3 seconds
- until @a <= 1
- {
- my $k = ([\>=] @a).first({ .not }, :k) // @a.elems;
- @decreasing.push: @a.splice(0, $k, @a[$k-1]);
-
- $k = ([\<=] @a).first({ .not }, :k) // @a.elems;
- @increasing.push: @a.splice(0, $k, @a[$k-1]);
- }
-
- @a = @decreasing Z @increasing;
+# This is ugly but I was determined to use a regular expression 🙄
+sub widest-valley(*@n)
+{
+ my %h = -1 => 'D', 0 => 'F', 1 => 'U';
+ my $s = @n.rotor(2 => -1).map({ %h{(.[1] - .[0]).sign } }).join;
+ my $h = 0;
- my $a := gather for @a
+ my $r := gather for $s ~~ m:g/ U (F*) D / -> $m
{
- pop .[0];
- take .comb(/\d+/)
+ take ($h, $m.to.pred);
+ $h = $m.to.pred - ($m[0] // '').chars;
+ LAST { take ($h, $s.chars) }
}
- $a[$_] given $a.cache>>.elems.maxpairs.first.key
+ my $m := $r>>.minmax;
+ my $k = $m>>.elems>>.flat.maxpairs.first.key;
+ @n[ $m[$k] ]
}