diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-08-07 00:15:28 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-07 00:15:28 +0100 |
| commit | 81bca6ba05687a59159830f37f18ea99184aea87 (patch) | |
| tree | ae166089eca52a67960592b3c30027192334a4fe | |
| parent | e7af7945d4c013391a457bb5abb5e9493ce9d358 (diff) | |
| parent | a778113c65f30fac0a4e0d816fc66d7944e11645 (diff) | |
| download | perlweeklychallenge-club-81bca6ba05687a59159830f37f18ea99184aea87.tar.gz perlweeklychallenge-club-81bca6ba05687a59159830f37f18ea99184aea87.tar.bz2 perlweeklychallenge-club-81bca6ba05687a59159830f37f18ea99184aea87.zip | |
Merge pull request #4667 from fluca1978/pwc124
Pwc124
| -rw-r--r-- | challenge-124/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-124/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-124/luca-ferrari/raku/ch-1.p6 | 53 | ||||
| -rw-r--r-- | challenge-124/luca-ferrari/raku/ch-2.p6 | 29 |
4 files changed, 84 insertions, 0 deletions
diff --git a/challenge-124/luca-ferrari/blog-1.txt b/challenge-124/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..6974ef53c3 --- /dev/null +++ b/challenge-124/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/08/04/PerlWeeklyChallenge124.html#task1 diff --git a/challenge-124/luca-ferrari/blog-2.txt b/challenge-124/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..74902e8534 --- /dev/null +++ b/challenge-124/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/08/04/PerlWeeklyChallenge124.html#task2 diff --git a/challenge-124/luca-ferrari/raku/ch-1.p6 b/challenge-124/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..c0f70bdb64 --- /dev/null +++ b/challenge-124/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,53 @@ +#!raku + +class Line { + has $.char = '^'; + has $.start = 0; + has $.end = 0; + has $.pad-with-blanks = True; + + method draw() { + my $line = ' ' x $!start ~ $!char; +# $line ~= $!start ~ "<>" ~ $!end; + if ( $!pad-with-blanks ) { + $line ~= ' ' x ( $!end - $!start - 1 ) ~ $!char; + } + else { + $line ~= $!char x ( $!end - $!start ); + } + + say $line; + + } +} + + +sub MAIN() { + + my @lines; + my $size = 5; + my ( $start, $end ) = $size, $size * 2 - 1; + @lines.push: Line.new( start => $start, end => $end, pad-with-blanks => False ); + while ( $start > 0 ) { + @lines.push: Line.new( start => --$start, end => ++$end ); + } + + # add three lines + @lines.push: Line.new( start => 0, end => $end ) for 1 .. 3; + + # decreasing part + while ( $start < $size ) { + @lines.push: Line.new( start => ++$start, end => --$end ); + } + + # final line + @lines.push: Line.new( start => $start, end => $end, pad-with-blanks => False ); + + # add three lines with a single char + @lines.push: Line.new( start => $start + $size / 2, end => $start + $size / 2, pad-with-blanks => False ) for 1 .. 3; + @lines.push: Line.new( start => $start, end => $end, pad-with-blanks => False ); + @lines.push: Line.new( start => $start + $size / 2, end => $start + $size / 2, pad-with-blanks => False ) for 1 .. 3; + + .draw for @lines + +} diff --git a/challenge-124/luca-ferrari/raku/ch-2.p6 b/challenge-124/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..aab0ff612d --- /dev/null +++ b/challenge-124/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,29 @@ +#!raku + +sub MAIN( *@S where { @S.elems == @S.grep( * ~~ Int ).elems && @S.elems > 0 } ) { + my $split-pos = do given @S.elems %% 2 { + when .so { @S.elems / 2 } + default { ( @S.elems - 1 ) / 2 } + }; + + my $min-diff; + my $sum = ( @S.sum / 2 ).Int.floor; + my @solution; + + for @S.combinations: $split-pos -> @current-set { + my $diff = ( $sum - @current-set.sum ).Int.abs; + if ( ! $min-diff || $min-diff > $diff ) { + @solution = @current-set.clone; + $min-diff = $diff; + } + } + + # assume each number appears one and only one + my @anti-solution; + for @S { + @anti-solution.push: $_ if ! @solution.grep( $_ ); + } + + say "Sets are { @solution.join( ',' ) } and { @anti-solution.join( ',' ) }"; +} + |
