aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Proctor <simon.proctor@zoopla.co.uk>2020-04-07 13:39:52 +0100
committerSimon Proctor <simon.proctor@zoopla.co.uk>2020-04-07 13:39:52 +0100
commit6892c9a72b70d5ff780e68c06a9b27a590b680eb (patch)
tree9a9187d4ebdd884f065fc76c4995b3eaa566472e
parent987bc609faf9e2478e0c84aef7e51255f1c84b7c (diff)
downloadperlweeklychallenge-club-6892c9a72b70d5ff780e68c06a9b27a590b680eb.tar.gz
perlweeklychallenge-club-6892c9a72b70d5ff780e68c06a9b27a590b680eb.tar.bz2
perlweeklychallenge-club-6892c9a72b70d5ff780e68c06a9b27a590b680eb.zip
Added a couple of different strategies
-rw-r--r--challenge-055/simon-proctor/raku/ch-2-2.p646
-rw-r--r--challenge-055/simon-proctor/raku/ch-2-3.p663
2 files changed, 109 insertions, 0 deletions
diff --git a/challenge-055/simon-proctor/raku/ch-2-2.p6 b/challenge-055/simon-proctor/raku/ch-2-2.p6
new file mode 100644
index 0000000000..327202b184
--- /dev/null
+++ b/challenge-055/simon-proctor/raku/ch-2-2.p6
@@ -0,0 +1,46 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+# Second attempt at this using a different plan
+#| Given a list of integers return all the wave sorted lists
+multi sub MAIN( *@input where { $_.elems >= 2 && $_.all ~~ Int } ) {
+ .join(",").say for find-waves( [], @input );
+}
+
+# Start state
+multi sub find-waves( [], @input is copy ) {
+ my @res;
+ for @input.kv -> $i, $val {
+ my @poss = @input[0..^$i,$i^..*-1].flat;
+ for find-waves( [$val], @poss, 'gte' ).grep( { defined $_ } ) -> @r {
+ @res.push( @r );
+ }
+ }
+ return @res;
+}
+
+# End state
+multi sub find-waves( @i, [] where { True }, 'gte' ) { ( @i, ); }
+multi sub find-waves( @i, [] where { True }, 'lte' ) { ( @i, ); }
+
+# Fail States
+multi sub find-waves( @i, @p where { @i[*-1] < all(@p) }, 'gte' ) {}
+multi sub find-waves( @i, @p where { @i[*-1] > all(@p) }, 'lte' ) {}
+
+# Possible State
+multi sub find-waves( @output is copy, @input is copy, $test ) {
+ my @out;
+
+ my &test = $test ~~ 'gte' ?? &infix:«>=» !! &infix:«<=»;
+
+ for @input.kv -> $i, $val {
+ next unless &test( @output[*-1], $val );
+ my @poss = @input[0..^$i,$i^..*-1].flat;
+
+ for find-waves( [|@output, $val], @poss, $test ~~ 'gte' ?? 'lte' !! 'gte' ).grep( { defined $_ } ) -> @r {
+ @out.push( @r );
+ }
+ }
+ return @out;
+}
diff --git a/challenge-055/simon-proctor/raku/ch-2-3.p6 b/challenge-055/simon-proctor/raku/ch-2-3.p6
new file mode 100644
index 0000000000..f8fe36709b
--- /dev/null
+++ b/challenge-055/simon-proctor/raku/ch-2-3.p6
@@ -0,0 +1,63 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+# Attempt the third
+#| Given a list of integers return all the wave sorted lists
+multi sub MAIN( *@input where { $_.elems >= 2 && $_.all ~~ Int } ) {
+ my $input-channel = Channel.new;
+ my $output-channel = Channel.new;
+ my $messages = Supplier.new;
+ my $reader = $messages.Supply;
+
+ my $count = 0;
+ for @input.kv -> $i, $val {
+ my @poss = @input[0..^$i,$i^..*-1].flat;
+ $input-channel.send( [ [$val], @poss, 'gte' ] );
+ $count++;
+ }
+
+ await start react {
+ whenever $reader -> $change {
+ $count += $change;
+ if ( $count == 0 ) {
+ $input-channel.close;
+ $output-channel.close;
+ $messages.done;
+ }
+ }
+ start react {
+ whenever $output-channel -> @out {
+ @out.join(",").say;
+ }
+ }
+ start react {
+ whenever $input-channel -> ( @i, @p, $t ) {
+ find-waves( @i, @p, $t, $input-channel, $output-channel, $messages );
+ }
+ }
+ }
+}
+
+# End state
+multi sub find-waves( @i, [] where { True }, 'gte', $, $out, $sup ) { $out.send( @i ); $sup.emit(-1); }
+multi sub find-waves( @i, [] where { True }, 'lte', $, $out, $sup ) { $out.send( @i ); $sup.emit(-1); }
+
+# Fail States
+multi sub find-waves( @i, @p where { @i[*-1] < all(@p) }, 'gte', $, $, $sup ) { $sup.emit(-1); }
+multi sub find-waves( @i, @p where { @i[*-1] > all(@p) }, 'lte', $, $, $sup ) { $sup.emit(-1);}
+
+# Possible State
+multi sub find-waves( @output is copy, @input is copy, $test, $in, $, $sup ) {
+ my @out;
+
+ my &test = $test ~~ 'gte' ?? &infix:«>=» !! &infix:«<=»;
+
+ for @input.kv -> $i, $val {
+ next unless &test( @output[*-1], $val );
+ my @poss = @input[0..^$i,$i^..*-1].flat;
+ $sup.emit(1);
+ $in.send( [ [|@output, $val], @poss, $test ~~ 'gte' ?? 'lte' !! 'gte' ] );
+ }
+ $sup.emit(-1);
+}