From 6d5027a7cbf1bc0c53b0522152e662fff5def033 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 20 Jan 2020 10:44:58 +0000 Subject: Challenge number 1. Functional Raku for the win --- challenge-044/simon-proctor/raku/ch-1.p6 | 58 ++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 challenge-044/simon-proctor/raku/ch-1.p6 diff --git a/challenge-044/simon-proctor/raku/ch-1.p6 b/challenge-044/simon-proctor/raku/ch-1.p6 new file mode 100644 index 0000000000..6766323b24 --- /dev/null +++ b/challenge-044/simon-proctor/raku/ch-1.p6 @@ -0,0 +1,58 @@ +#!/usr/bin/env perl6 + +use v6.d; + +multi sub concat( \a, \b ) { a ~ b } +multi sub concat( \a, @b ) { ( a ~ @b[0], @b[1..*] ) } + +my %ops-map = ( + '+' => &infix:<+>, + '-' => &infix:<->, + ',' => &infix:<,>, + '~' => &concat, +); + +multi sub selections(0, *@) is pure { + return []; +} + +multi sub selections(1,*@ops) is pure { + return [[@ops[0]],[@ops[1]]]; +} + +multi sub selections(UInt \c, *@ops) is pure { + lazy gather { + my @recur = selections(c-1, @ops); + for selections(1, @ops) -> @prefix { + for @recur -> @list { + take [|@prefix, |@list]; + } + } + } +} + +multi sub apply-ops( @list where *.elems == 2, @ops ) { + return @ops[0]( @list[0], @list[1] ); +} + +multi sub apply-ops( @list, @ops ) { + return @ops[0](@list[0],apply-ops( @list[1..*], @ops[1..*] ) ); +} + +sub display( @nums, @ops ) { + my $result = @nums.map( -> $v { $v ~ (@ops ?? @ops.shift !! '') } ).join(""); +} + +my @list = "1".."9"; + +my @inputs = selections(@list.elems-1, ',', '~').map( -> @sel { apply-ops( @list, @sel.map( { %ops-map{$_} } ) ).flat } ); + +for @inputs -> @test { + for selections(@test.elems-1, '+', '-') -> @ops { + my $result = apply-ops( @test, @ops.map( { %ops-map{$_} } ) ); + if ( $result ~~ 100 ) { + say "{display(@test,@ops)} = $result"; + exit; + } + } +} -- cgit From 7f7cf93d3e2c5acc32cf4459b77af8b82d64a615 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 20 Jan 2020 11:56:06 +0000 Subject: Using recursion back from 100 to 1 to get the 8 steps needed to get from 1 to 100 --- challenge-044/simon-proctor/raku/ch-2.p6 | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 challenge-044/simon-proctor/raku/ch-2.p6 diff --git a/challenge-044/simon-proctor/raku/ch-2.p6 b/challenge-044/simon-proctor/raku/ch-2.p6 new file mode 100644 index 0000000000..551c8ca0aa --- /dev/null +++ b/challenge-044/simon-proctor/raku/ch-2.p6 @@ -0,0 +1,17 @@ +#!/usr/bin/env perl6 + +use v6.d; + +multi sub recur( Int() $v where * %% 2 ) { + return recur( $v / 2 ), " * 2"; +} + +multi sub recur( Int() $v ) { + return "((", recur( $v - 1 ), ") + 1)"; +} + +multi sub recur($ where * ~~ 1) { + return "1"; +} + +say "{recur( 100 ).flat.join('')} == 100"; -- cgit From 3cfe1aa8fef811ba8b5cd4764217b632982376a7 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 20 Jan 2020 12:06:56 +0000 Subject: Use Div so we have and Int so we can simplify the termination --- challenge-044/simon-proctor/raku/ch-2.p6 | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/challenge-044/simon-proctor/raku/ch-2.p6 b/challenge-044/simon-proctor/raku/ch-2.p6 index 551c8ca0aa..3d87ee3192 100644 --- a/challenge-044/simon-proctor/raku/ch-2.p6 +++ b/challenge-044/simon-proctor/raku/ch-2.p6 @@ -2,16 +2,17 @@ use v6.d; -multi sub recur( Int() $v where * %% 2 ) { - return recur( $v / 2 ), " * 2"; +multi recur(1) { + return "1"; } -multi sub recur( Int() $v ) { - return "((", recur( $v - 1 ), ") + 1)"; +multi recur( \v where * %% 2 ) { + return recur( v div 2 ), " * 2"; } -multi sub recur($ where * ~~ 1) { - return "1"; +multi recur( \v ) { + return "((", recur( v - 1 ), ") + 1)"; } + say "{recur( 100 ).flat.join('')} == 100"; -- cgit From f2a0bff3a57f399909dfba57b9dfd546fdf30e5d Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 20 Jan 2020 16:04:49 +0000 Subject: If we want to add more ops we can --- challenge-044/simon-proctor/raku/ch-1.p6 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-044/simon-proctor/raku/ch-1.p6 b/challenge-044/simon-proctor/raku/ch-1.p6 index 6766323b24..e0367d5842 100644 --- a/challenge-044/simon-proctor/raku/ch-1.p6 +++ b/challenge-044/simon-proctor/raku/ch-1.p6 @@ -17,7 +17,7 @@ multi sub selections(0, *@) is pure { } multi sub selections(1,*@ops) is pure { - return [[@ops[0]],[@ops[1]]]; + return [ @ops.map( -> $v { [$v] } ) ]; } multi sub selections(UInt \c, *@ops) is pure { -- cgit From bed16c13a8849ab1a9dc5934ccb72f90182c480d Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 20 Jan 2020 16:08:28 +0000 Subject: Find *all* the options --- challenge-044/simon-proctor/raku/ch-1.p6 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/challenge-044/simon-proctor/raku/ch-1.p6 b/challenge-044/simon-proctor/raku/ch-1.p6 index e0367d5842..5ec6641727 100644 --- a/challenge-044/simon-proctor/raku/ch-1.p6 +++ b/challenge-044/simon-proctor/raku/ch-1.p6 @@ -52,7 +52,8 @@ for @inputs -> @test { my $result = apply-ops( @test, @ops.map( { %ops-map{$_} } ) ); if ( $result ~~ 100 ) { say "{display(@test,@ops)} = $result"; - exit; } } } + + -- cgit From 13ec7fec2d9674a41bd17bbc597eb394d3b73c55 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 20 Jan 2020 16:18:01 +0000 Subject: Change ordering of apply ops to get valid output --- challenge-044/simon-proctor/raku/ch-1.p6 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-044/simon-proctor/raku/ch-1.p6 b/challenge-044/simon-proctor/raku/ch-1.p6 index 5ec6641727..d99c7836bd 100644 --- a/challenge-044/simon-proctor/raku/ch-1.p6 +++ b/challenge-044/simon-proctor/raku/ch-1.p6 @@ -3,7 +3,7 @@ use v6.d; multi sub concat( \a, \b ) { a ~ b } -multi sub concat( \a, @b ) { ( a ~ @b[0], @b[1..*] ) } +multi sub concat( @a, \b ) { ( @a[0..*-2], @a[*-1] ~ b ) } my %ops-map = ( '+' => &infix:<+>, @@ -36,7 +36,7 @@ multi sub apply-ops( @list where *.elems == 2, @ops ) { } multi sub apply-ops( @list, @ops ) { - return @ops[0](@list[0],apply-ops( @list[1..*], @ops[1..*] ) ); + return @ops[*-1](apply-ops( @list[0..*-2], @ops[0..*-2] ), @list[*-1] ); } sub display( @nums, @ops ) { -- cgit