From f7774a6c62b2d26c22dd9cd3859bfadeb831ecd9 Mon Sep 17 00:00:00 2001 From: "Markus \"Holli\" Holzer" Date: Mon, 9 Mar 2020 20:09:18 +0100 Subject: better --- challenge-051/markus-holzer/ch-2.p6 | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/challenge-051/markus-holzer/ch-2.p6 b/challenge-051/markus-holzer/ch-2.p6 index e8b4096159..70870b59eb 100644 --- a/challenge-051/markus-holzer/ch-2.p6 +++ b/challenge-051/markus-holzer/ch-2.p6 @@ -1,10 +1,11 @@ use Terminal::ANSIColor; +use Terminal::Table; sub MAIN( Int $from, Int $to, Bool :$only = False ) { my @colors = ; - print "$_," for gather for $from..$to -> $n + print "$_, " for gather for $from..$to -> $n { my @subsets = consecutive-combinations( $n.comb ); my @products = @subsets.map({ [*] $_ }); @@ -13,17 +14,14 @@ sub MAIN( Int $from, Int $to, Bool :$only = False ) take color($color) ~ $n ~ color('reset') if $colorful or !$only; - }; + } } sub consecutive-combinations( @n ) { - my sub keys-of( @p ) { @p.map: *.key } - my sub values-of( @p ) { @p.map: *.value } - - my multi sub is-consecutive( $a, $b ) { $a + 1 == $b } - my multi sub is-consecutive( @n ) { @n.elems == 1 or [[&is-consecutive]] @n } + my sub keys-of( @p ) { @p.map: *.key } + my sub values-of( @p ) { @p.map: *.value } @n .pairs @@ -33,6 +31,12 @@ sub consecutive-combinations( @n ) ; } +my sub is-consecutive( @n ) +{ + @n.elems == 1 + or + not @n.rotor(2 => -1).first({ .[0] + 1 == .[1] }) +} -- cgit From 735f6d0faeb7df7f4f58005daaa64366967f1708 Mon Sep 17 00:00:00 2001 From: "Markus \"Holli\" Holzer" Date: Mon, 9 Mar 2020 21:29:07 +0100 Subject: this is nicer --- challenge-051/markus-holzer/raku/ch-2.p6 | 48 +++++++++++++++++++------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/challenge-051/markus-holzer/raku/ch-2.p6 b/challenge-051/markus-holzer/raku/ch-2.p6 index 70870b59eb..03b149de78 100644 --- a/challenge-051/markus-holzer/raku/ch-2.p6 +++ b/challenge-051/markus-holzer/raku/ch-2.p6 @@ -1,28 +1,47 @@ use Terminal::ANSIColor; -use Terminal::Table; sub MAIN( Int $from, Int $to, Bool :$only = False ) { - my @colors = ; - print "$_, " for gather for $from..$to -> $n + print "$_, " for gather for $from..$to -> $n { - my @subsets = consecutive-combinations( $n.comb ); - my @products = @subsets.map({ [*] $_ }); - my $colorful = @products.elems == @products.unique.elems; - my $color = $colorful ?? @colors.pick !! 'white'; - - take color($color) ~ $n ~ color('reset') - if $colorful or !$only; + my $is-colorful = is-colorful( $n ); + take colorize( $n, $is-colorful ) + if $is-colorful or !$only; } } +sub colorize( $n, $colorful ) +{ + state @colors = ; + my $color = $colorful ?? @colors.pick !! 'white'; + color($color) ~ $n ~ color('reset'); +} + +sub is-colorful( $n ) +{ + my @digits = $n.comb; + + # save some work + return False + if ( @digits.repeated ) + or ( $n > 1 && '1' (elem) @digits ); + + not consecutive-combinations( @digits ) + .map({ [*] $_ }) + .repeated + ; +} + sub consecutive-combinations( @n ) { my sub keys-of( @p ) { @p.map: *.key } my sub values-of( @p ) { @p.map: *.value } + my sub is-consecutive( @n ) { + not @n.rotor(2 => -1).first( -> ($a, $b) { $a + 1 != $b }) } + @n .pairs .combinations(1..*) @@ -31,12 +50,3 @@ sub consecutive-combinations( @n ) ; } -my sub is-consecutive( @n ) -{ - @n.elems == 1 - or - not @n.rotor(2 => -1).first({ .[0] + 1 == .[1] }) -} - - - -- cgit From 53db8e9ae548aaf334e65c7822cec534cba22dfe Mon Sep 17 00:00:00 2001 From: "Markus \"Holli\" Holzer" Date: Mon, 9 Mar 2020 21:30:34 +0100 Subject: this is nicer --- challenge-051/markus-holzer/raku/ch-2.p6 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-051/markus-holzer/raku/ch-2.p6 b/challenge-051/markus-holzer/raku/ch-2.p6 index 03b149de78..cd2333fdf8 100644 --- a/challenge-051/markus-holzer/raku/ch-2.p6 +++ b/challenge-051/markus-holzer/raku/ch-2.p6 @@ -7,7 +7,7 @@ sub MAIN( Int $from, Int $to, Bool :$only = False ) { my $is-colorful = is-colorful( $n ); take colorize( $n, $is-colorful ) - if $is-colorful or !$only; + if $is-colorful or not $only; } } -- cgit From aaa70404bcbfe6338394b750b8f4ba57d7cd5f28 Mon Sep 17 00:00:00 2001 From: "Markus \"Holli\" Holzer" Date: Mon, 9 Mar 2020 21:33:23 +0100 Subject: this is nicer --- challenge-051/markus-holzer/raku/ch-2.p6 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/challenge-051/markus-holzer/raku/ch-2.p6 b/challenge-051/markus-holzer/raku/ch-2.p6 index cd2333fdf8..ae8a893b1d 100644 --- a/challenge-051/markus-holzer/raku/ch-2.p6 +++ b/challenge-051/markus-holzer/raku/ch-2.p6 @@ -48,5 +48,4 @@ sub consecutive-combinations( @n ) .grep({ is-consecutive( .&keys-of ) }) .map({ .&values-of }) ; -} - +} \ No newline at end of file -- cgit From 229b8dae509eecd37df5471182b57b5581bd0a64 Mon Sep 17 00:00:00 2001 From: "Markus \"Holli\" Holzer" Date: Mon, 9 Mar 2020 21:36:15 +0100 Subject: formatting --- challenge-051/markus-holzer/raku/ch-2.p6 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/challenge-051/markus-holzer/raku/ch-2.p6 b/challenge-051/markus-holzer/raku/ch-2.p6 index ae8a893b1d..ced8f024ed 100644 --- a/challenge-051/markus-holzer/raku/ch-2.p6 +++ b/challenge-051/markus-holzer/raku/ch-2.p6 @@ -16,7 +16,7 @@ sub colorize( $n, $colorful ) { state @colors = ; my $color = $colorful ?? @colors.pick !! 'white'; - color($color) ~ $n ~ color('reset'); + color( $color ) ~ $n ~ color( 'reset' ); } sub is-colorful( $n ) @@ -40,11 +40,11 @@ sub consecutive-combinations( @n ) my sub values-of( @p ) { @p.map: *.value } my sub is-consecutive( @n ) { - not @n.rotor(2 => -1).first( -> ($a, $b) { $a + 1 != $b }) } + not @n.rotor( 2 => -1 ).first( -> ($a, $b) { $a + 1 != $b }) } @n .pairs - .combinations(1..*) + .combinations( 1..* ) .grep({ is-consecutive( .&keys-of ) }) .map({ .&values-of }) ; -- cgit