From 6892c9a72b70d5ff780e68c06a9b27a590b680eb Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Tue, 7 Apr 2020 13:39:52 +0100 Subject: Added a couple of different strategies --- challenge-055/simon-proctor/raku/ch-2-2.p6 | 46 ++++++++++++++++++++++ challenge-055/simon-proctor/raku/ch-2-3.p6 | 63 ++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 challenge-055/simon-proctor/raku/ch-2-2.p6 create mode 100644 challenge-055/simon-proctor/raku/ch-2-3.p6 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); +} -- cgit