diff options
| author | Luca Ferrari <fluca1978@gmail.com> | 2024-08-08 09:53:05 +0200 |
|---|---|---|
| committer | Luca Ferrari <fluca1978@gmail.com> | 2024-08-08 16:25:12 +0200 |
| commit | c2c0542e6b26e4df6d839ad309f826894682d9fc (patch) | |
| tree | 623c025b3a549664ed4c6d5a7df3232adf9c43cd | |
| parent | ff6d24bcafad14f48b83206b73c7638599bef9ca (diff) | |
| download | perlweeklychallenge-club-c2c0542e6b26e4df6d839ad309f826894682d9fc.tar.gz perlweeklychallenge-club-c2c0542e6b26e4df6d839ad309f826894682d9fc.tar.bz2 perlweeklychallenge-club-c2c0542e6b26e4df6d839ad309f826894682d9fc.zip | |
PWC 281
Task 1 Raku done
Task 2 Raku done
| -rw-r--r-- | challenge-281/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-281/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-281/luca-ferrari/pljava/pom.xml | 72 | ||||
| -rw-r--r-- | challenge-281/luca-ferrari/raku/ch-1.raku | 23 | ||||
| -rw-r--r-- | challenge-281/luca-ferrari/raku/ch-2.raku | 81 |
5 files changed, 106 insertions, 72 deletions
diff --git a/challenge-281/luca-ferrari/blog-1.txt b/challenge-281/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..94e5e0284e --- /dev/null +++ b/challenge-281/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/08/08/PerlWeeklyChallenge281.html#task1 diff --git a/challenge-281/luca-ferrari/blog-2.txt b/challenge-281/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..ed57216f56 --- /dev/null +++ b/challenge-281/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/08/08/PerlWeeklyChallenge281.html#task2 diff --git a/challenge-281/luca-ferrari/pljava/pom.xml b/challenge-281/luca-ferrari/pljava/pom.xml deleted file mode 100644 index 96ca25649f..0000000000 --- a/challenge-281/luca-ferrari/pljava/pom.xml +++ /dev/null @@ -1,72 +0,0 @@ - -<project - xmlns="http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > - <modelVersion>4.0.0</modelVersion> - - <groupId>PWC</groupId> - <artifactId> - PWC280 - </artifactId> - <version> - 1 - </version> - - <name>Perl Weekly Challenge 280 with package PWC280</name> - <description>Implementation of the tasks in PL/Java for PWC 280</description> - - <properties> - <project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding> - </properties> - - <dependencies> - <dependency> - <groupId>org.postgresql</groupId> - <artifactId>pljava-api</artifactId> - <version>1.6.6</version> - </dependency> - </dependencies> - - <build> - <plugins> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-compiler-plugin</artifactId> - <version>3.8.1</version> - <configuration> - <release>9</release> - </configuration> - </plugin> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-jar-plugin</artifactId> - <version>2.6</version> - <configuration> - <archive> - <manifest> - <!-- This identifies and version-stamps the jar. - Not essential, but easy and useful. --> - <addDefaultImplementationEntries> - true - </addDefaultImplementationEntries> - </manifest> - - <manifestSections> - <!-- This identifies a file in the jar named - pljava.ddr as an SQLJDeploymentDescriptor. --> - <manifestSection> - <name>pljava.ddr</name> - <manifestEntries> - <SQLJDeploymentDescriptor> - true - </SQLJDeploymentDescriptor> - </manifestEntries> - </manifestSection> - </manifestSections> - </archive> - </configuration> - </plugin> - </plugins> - </build> -</project> diff --git a/challenge-281/luca-ferrari/raku/ch-1.raku b/challenge-281/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..7da9df1ce9 --- /dev/null +++ b/challenge-281/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,23 @@ +#!raku + +# +# Perl Weekly Challenge 281 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-281> +# + +sub MAIN( Str $coordinates where { $coordinates ~~ / ^ <[a .. h]> <[ 1 .. 8 ]> $/ } ) { + my %chessboard; + my $value = False; + for 1 .. 8 -> $row { + $value = $row %% 2 ?? True !! False; + for 'a' .. 'h' -> $column { + %chessboard{ $column ~ $row } = $value; + $value = ! $value; + } + } + + %chessboard{ $coordinates }.say; + +} diff --git a/challenge-281/luca-ferrari/raku/ch-2.raku b/challenge-281/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..6f1136fcda --- /dev/null +++ b/challenge-281/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,81 @@ +#!raku + +# +# Perl Weekly Challenge 281 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-281> +# + + +sub compute-end-points( $start ) { + my @end-points; + my @valid-columns = 'a' .. 'h'; + + $start ~~ / ^ $<column>=( <[a..h]> ) $<row>=( <[1..8]> ) $ /; + my $row = $<row>.Int; + my $column = $<column>.Str; + + return Nil if ( $row > 8 || $row < 1 || ! @valid-columns.grep( { $_ ~~ $column } ) ); + + # at which index is the current column labeled with a letter? + $column = @valid-columns.grep( { $_ ~~ $column }, :k )[ 0 ]; + + my @moves = [ +1, -2 ], [ +1, +2 ], + [ +2, -1 ], [ +2, +1 ], + [ -1, -2 ], [ -1, +2 ], + [ -2, -1 ], [ -2, +1 ]; + + @end-points = @moves.map( + { [ @valid-columns[ $_[ 0 ] + $column ], $_[ 1 ] + $row ] } ) + .grep( { $_[ 0 ] && @valid-columns.grep( * ~~ $_[ 0 ] ) && 1 <= $_[ 1 ] <= 8 } ) + .map( { $_[ 0 ] ~ $_[ 1 ] } ); + ; + return @end-points; +} + + +sub MAIN( Str $start, + Str $end, + :$verbose + where { $start !~~ $end + and $start ~~ / ^ <[a..h]> <[1..8]> $ / + and $end ~~ / ^ <[a..h]> <[1..8]> $ / } + ) { + + + my @end-points = compute-end-points( $start ); + + my @paths; + @paths = @end-points.map( $start ~ '-' ~ * ); + + my @new-paths; + my $found = False; + my $debug = 0; + while ( ! $found ) { + for @paths.sort -> $current-path { + my $next = $current-path.split( '-' )[ * - 1 ]; + my @ends = compute-end-points( $next ).grep( { $_ ne $start && $_ ne $next } ); + @new-paths.push: $current-path ~ '-' ~ $_ for @ends; + } + + @paths = @new-paths; + @new-paths = (); + $found = @paths.grep( * ~~ / $end $ / ); + + } + + + + my %possible-moves; + for @paths.grep( * ~~ / $end $ / ) { + %possible-moves{ $_.split( '-' ).elems }.push: $_; + } + + my $min-moves = %possible-moves.keys.min; + $min-moves.say; + if ( $verbose ) { + "possible moves:".say; + %possible-moves{ $min-moves }.sort.join( "\n" ).say; + } +} |
