diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-08-31 15:24:39 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-31 15:24:39 +0100 |
| commit | a249362451c86333100c8fb9c8c5d9227b27d5cf (patch) | |
| tree | 70060be0e7f549af3ee99d263e4a6aff34ed367c | |
| parent | 7baa907f987cbfc959037ceb362a13d52ab6a968 (diff) | |
| parent | 2355c56e832497e48773e4f8e095a2ed7c7c9041 (diff) | |
| download | perlweeklychallenge-club-a249362451c86333100c8fb9c8c5d9227b27d5cf.tar.gz perlweeklychallenge-club-a249362451c86333100c8fb9c8c5d9227b27d5cf.tar.bz2 perlweeklychallenge-club-a249362451c86333100c8fb9c8c5d9227b27d5cf.zip | |
Merge pull request #2181 from fluca1978/pwc76
Pwc76
| -rw-r--r-- | challenge-076/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-076/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-076/luca-ferrari/raku/ch-1.p6 | 26 | ||||
| -rw-r--r-- | challenge-076/luca-ferrari/raku/ch-2.p6 | 77 | ||||
| -rw-r--r-- | challenge-076/luca-ferrari/raku/grid.txt | 19 |
5 files changed, 124 insertions, 0 deletions
diff --git a/challenge-076/luca-ferrari/blog-1.txt b/challenge-076/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..afb7288a8a --- /dev/null +++ b/challenge-076/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2020/08/31/PerlWeeklyChallenge76.html#task1 diff --git a/challenge-076/luca-ferrari/blog-2.txt b/challenge-076/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..6a198e8433 --- /dev/null +++ b/challenge-076/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2020/08/31/PerlWeeklyChallenge76.html#task2 diff --git a/challenge-076/luca-ferrari/raku/ch-1.p6 b/challenge-076/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..d9c81c40a9 --- /dev/null +++ b/challenge-076/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,26 @@ +#!raku + + +sub MAIN( Int:D $N where { $N > 1 } ) { + + # get primes excluding 1 + my @primes = ( 1 ^..^ $N ).grep( *.is-prime ).sort; + + my @sums; + my $how-many = 1; + while ( @sums.elems == 0 ) { + $how-many++; # start with summing two numbers + for @primes.permutations -> @checking { + for @checking.rotor( $how-many ) { + my @current-numbers = $_.sort; + my $sum = [+] @current-numbers; + @sums.push: @current-numbers if ( $sum == $N && ! @sums.grep( * ~~ @current-numbers ) ); + } + } + } + + + # print the result + "$N minimum sum is made by: ".say; + .join( ' + ' ).say for @sums ; +} diff --git a/challenge-076/luca-ferrari/raku/ch-2.p6 b/challenge-076/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..76ea47631e --- /dev/null +++ b/challenge-076/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,77 @@ +#!raku + + + +sub diagonal-words( @grid-chars, $up-to-down = True, $left-to-right = True ) { + my @diagonals; + my ( $row, $column ) = $up-to-down ?? 0 !! @grid-chars.elems - 1, + $left-to-right ?? 0 !! @grid-chars[ 0 ].elems - 1; + + my ( $row-increment, $column-increment ) = $up-to-down ?? 1 !! -1, + $left-to-right ?? 1 !! -1; + + my ( $last-row, $last-column ) = $row, $column; + my @word; + while ( $last-row ~~ 0 ..^ @grid-chars.elems + && $last-column ~~ 0 ..^ @grid-chars[0].elems ) { + + ( $last-row, $last-column ) = $row, $column; + while ( $last-column ~~ 0 ..^ @grid-chars[0].elems ) { + @word = (); + while ( $row ~~ 0 ..^ @grid-chars.elems + && $column ~~ 0 ..^ @grid-chars[0].elems ) { + @word.push: @grid-chars[ $row ][ $column ]; + $row += $row-increment; + $column += $column-increment; + } + + @diagonals.push: @word.join, @word.join.flip; + $last-column += $column-increment; + ($row, $column) = $last-row, $last-column; + } + + } + + @diagonals.grep( *.chars > 2 ); + +} + + + + +sub MAIN( $grid-file-name = 'grid.txt', + $word-file-name = '/usr/share/dict/words', + $min-length = 3 ) { + say "Searching words from $word-file-name into grid $grid-file-name"; + my @found-words; + + + # get all the lines in the grid lowercase + my @grid-chars = $grid-file-name.IO.lines.map( *.lc.split( /\s/, :skip-empty ).Array ).Array; + + my ( @horizontals, @verticals, @diagonals ); + for @grid-chars { + @horizontals.push: .join, .join.flip; + } + + for ( [Z] @grid-chars ) { + @verticals.push: .join, join.flip; + } + + + @diagonals.push: diagonal-words( @grid-chars, True, True ); + @diagonals.push: diagonal-words( @grid-chars, True, False ); + @diagonals.push: diagonal-words( @grid-chars, False, True ); + @diagonals.push: diagonal-words( @grid-chars, False, False ); + + + for $word-file-name.IO.lines { + next if .chars < $min-length; + my $current-word = $_.lc; + @found-words.push: $current-word if ( @diagonals.grep( * ~~ / $current-word / ) + || @horizontals.grep( * ~~ / $current-word / ) + || @verticals.grep( * ~~ / $current-word / ) ); + } + + say "Found { @found-words.elems } words: { @found-words.join( ',' ) }"; +} diff --git a/challenge-076/luca-ferrari/raku/grid.txt b/challenge-076/luca-ferrari/raku/grid.txt new file mode 100644 index 0000000000..31cf2e0fd8 --- /dev/null +++ b/challenge-076/luca-ferrari/raku/grid.txt @@ -0,0 +1,19 @@ +B I D E M I A T S U C C O R S T +L D E G G I W Q H O D E E H D P +U S E I R U B U T E A S L A G U +N G N I Z I L A I C O S C N U D +T G M I D S T S A R A R E I F G +S R E N M D C H A S I V E E L I +S C S H A E U E B R O A D M T E +H W O V L P E D D L A I U L S S +R Y O N L A S F C S T A O G O T +I G U S S R R U G O V A R Y O C +N R G P A T N A N G I L A M O O +E I H A C E I V I R U S E S E D +S E T S U D T T G A R L I C N H +H V R M X L W I U M S N S O T B +A E A O F I L C H T O D C A E U +Z S C D F E C A A I I R L N R F +A R I I A N Y U T O O O U T P F +R S E C I S N A B O S C N E R A +D R S M P C U U N E L T E S I L |
