From cec120c3bbaad86ffb94ed9e76f615926eef3bfc Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 31 Aug 2020 09:13:38 +0200 Subject: Challenge 1 dne. --- challenge-076/luca-ferrari/raku/ch-1.p6 | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 challenge-076/luca-ferrari/raku/ch-1.p6 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 ; +} -- cgit From 3cf66025e0b75a7b10b755fa25265886cff9b449 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 31 Aug 2020 12:33:10 +0200 Subject: Task 2 done. --- challenge-076/luca-ferrari/raku/ch-2.p6 | 78 ++++++++++++++++++++++++++++++++ challenge-076/luca-ferrari/raku/grid.txt | 19 ++++++++ 2 files changed, 97 insertions(+) create mode 100644 challenge-076/luca-ferrari/raku/ch-2.p6 create mode 100644 challenge-076/luca-ferrari/raku/grid.txt 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..987d82b26f --- /dev/null +++ b/challenge-076/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,78 @@ +#!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 word 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; + say @grid-chars; + + 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.unique.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 -- cgit From 5a4f6107043cdde823901d9ded1fc70df97852c6 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 31 Aug 2020 15:49:10 +0200 Subject: Remove unique in word count. --- challenge-076/luca-ferrari/raku/ch-2.p6 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-076/luca-ferrari/raku/ch-2.p6 b/challenge-076/luca-ferrari/raku/ch-2.p6 index 987d82b26f..221824e381 100644 --- a/challenge-076/luca-ferrari/raku/ch-2.p6 +++ b/challenge-076/luca-ferrari/raku/ch-2.p6 @@ -74,5 +74,5 @@ sub MAIN( $grid-file-name = 'grid.txt', || @verticals.grep( * ~~ / $current-word / ) ); } - say "Found { @found-words.unique.elems } words: { @found-words.join( ',' ) }"; + say "Found { @found-words.elems } words: { @found-words.join( ',' ) }"; } -- cgit From dd6c2790c488c5c8eedadaf9966a07f25077af5d Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 31 Aug 2020 15:49:36 +0200 Subject: Fix typo --- challenge-076/luca-ferrari/raku/ch-2.p6 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/challenge-076/luca-ferrari/raku/ch-2.p6 b/challenge-076/luca-ferrari/raku/ch-2.p6 index 221824e381..76ea47631e 100644 --- a/challenge-076/luca-ferrari/raku/ch-2.p6 +++ b/challenge-076/luca-ferrari/raku/ch-2.p6 @@ -42,13 +42,12 @@ sub diagonal-words( @grid-chars, $up-to-down = True, $left-to-right = True ) { sub MAIN( $grid-file-name = 'grid.txt', $word-file-name = '/usr/share/dict/words', $min-length = 3 ) { - say "Searching word from $word-file-name into grid $grid-file-name"; + 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; - say @grid-chars; my ( @horizontals, @verticals, @diagonals ); for @grid-chars { -- cgit From 2355c56e832497e48773e4f8e095a2ed7c7c9041 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 31 Aug 2020 16:14:07 +0200 Subject: Blog reference --- challenge-076/luca-ferrari/blog-1.txt | 1 + challenge-076/luca-ferrari/blog-2.txt | 1 + 2 files changed, 2 insertions(+) create mode 100644 challenge-076/luca-ferrari/blog-1.txt create mode 100644 challenge-076/luca-ferrari/blog-2.txt 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 -- cgit