diff options
| author | Luca Ferrari <fluca1978@gmail.com> | 2020-08-31 12:33:10 +0200 |
|---|---|---|
| committer | Luca Ferrari <fluca1978@gmail.com> | 2020-08-31 15:48:40 +0200 |
| commit | 3cf66025e0b75a7b10b755fa25265886cff9b449 (patch) | |
| tree | 8fbaa25a0fb6ffe8df999e6d700f7b231531cd2a | |
| parent | cec120c3bbaad86ffb94ed9e76f615926eef3bfc (diff) | |
| download | perlweeklychallenge-club-3cf66025e0b75a7b10b755fa25265886cff9b449.tar.gz perlweeklychallenge-club-3cf66025e0b75a7b10b755fa25265886cff9b449.tar.bz2 perlweeklychallenge-club-3cf66025e0b75a7b10b755fa25265886cff9b449.zip | |
Task 2 done.
| -rw-r--r-- | challenge-076/luca-ferrari/raku/ch-2.p6 | 78 | ||||
| -rw-r--r-- | challenge-076/luca-ferrari/raku/grid.txt | 19 |
2 files changed, 97 insertions, 0 deletions
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 |
