diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2024-08-06 23:29:43 +0100 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2024-08-06 23:29:43 +0100 |
| commit | 74d6f5b5d7b341e12055ea73d3e3e0576fdc2b18 (patch) | |
| tree | a3502587fe5f10ff341fba8561049a4d192f5054 | |
| parent | 3342cc3576aa1d6c59929fad1100ab1809e2b3c3 (diff) | |
| download | perlweeklychallenge-club-74d6f5b5d7b341e12055ea73d3e3e0576fdc2b18.tar.gz perlweeklychallenge-club-74d6f5b5d7b341e12055ea73d3e3e0576fdc2b18.tar.bz2 perlweeklychallenge-club-74d6f5b5d7b341e12055ea73d3e3e0576fdc2b18.zip | |
- Added solutions by Peter Meszaros.
- Added solutions by Ulrich Rieke.
- Added solutions by Steven Wilson.
36 files changed, 3448 insertions, 2636 deletions
diff --git a/challenge-281/peter-meszaros/perl/ch-1.pl b/challenge-281/peter-meszaros/perl/ch-1.pl new file mode 100644 index 0000000000..8cbcddaa71 --- /dev/null +++ b/challenge-281/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,56 @@ +#!/usr/bin/env perl +# +=head1 Task 1: Check Color +Submitted by: Mohammad Sajid Anwar +You are given coordinates, a string that represents the coordinates of a square +of the chessboard as shown below: + +-+-+-+-+-+-+-+-+ + 8 | |#| |#| |#| |#| + 7 |#| |#| |#| |#| | + 6 | |#| |#| |#| |#| + 5 |#| |#| |#| |#| | + 4 | |#| |#| |#| |#| + 3 |#| |#| |#| |#| | + 2 | |#| |#| |#| |#| + 1 |#| |#| |#| |#| | + +-+-+-+-+-+-+-+-+ + a b c d e f g h +Write a script to return true if the square is light, and false if the square +is dark. +=head2 Example 1 + Input: $coordinates = "d3" + Output: true +=head2 Example 2 + Input: $coordinates = "g5" + Output: false +=head2 Example 3 + Input: $coordinates = "e6" + Output: true +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + ['d3', 1, 'Example 1'], + ['g5', 0, 'Example 2'], + ['e6', 1, 'Example 3'], +]; + +sub check_color +{ + my $pos = shift; + + my ($x, $y) = split '', $pos; + + return (ord($x) + $y) % 2; +} + +for (@$cases) { + is(check_color($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-281/peter-meszaros/perl/ch-2.pl b/challenge-281/peter-meszaros/perl/ch-2.pl new file mode 100644 index 0000000000..b708fd4754 --- /dev/null +++ b/challenge-281/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,88 @@ +#!/usr/bin/env perl +# +=head1 Task 2: Knight's Move +Submitted by: Peter Campbell Smith +A Knight in chess can move from its current position to any square two rows or +columns plus one column or row away. So in the diagram below, if it starts a S, +it can move to any of the squares marked E. +Write a script which takes a starting position and an ending position and +calculates the least number of moves required. + +--+--+--+--+--+--+--+--+ + 8 |a8| | | | | | | | + 7 | | |E | |E | | | | + 6 | |E | | | |E | | | + 5 | | | |S | | | | | + 4 | |E | | | |E | | | + 3 | | |E | |E | | | | + 2 | | | | | | |g2| | + 1 | | | | | | | | | + +--+--+--+--+--+--+--+--+ + a b c d e f g h +=head2 Example 1 + Input: $start = 'g2', $end = 'a8' + Ouput: 4 + g2 -> e3 -> d5 -> c7 -> a8 +=head2 Example 2 + Input: $start = 'g2', $end = 'h2' + Ouput: 3 + g2 -> e3 -> f1 -> h2 +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [['g2', 'a8'], 4, 'Example 1'], + [['g2', 'h2'], 3, 'Example 2'], +]; + +sub knights_move +{ + my $s = $_[0]->[0]; + my $e = $_[0]->[1]; + + my @s = split '', $s; + my @e = split '', $e; + + $s[0] = ord($s[0]) - ord('a'); + $e[0] = ord($e[0]) - ord('a'); + $s[1]--; + $e[1]--; + + my $dx = [2, 2, -2, -2, 1, 1, -1, -1]; + my $dy = [1, -1, 1, -1, 2, -2, 2, -2]; + + my @queue = [$s[0], $s[1], 0]; + my $visited->[$s[0]]->[$s[1]] = 1; + + while (@queue) { + my $p = shift @queue; + my $x = $p->[0]; + my $y = $p->[1]; + my $v = $p->[2]; + if ($x == $e[0] && $y == $e[1]) { + return $v; + } + for my $i (0 .. 7) { + my $new_x = $p->[0] + $dx->[$i]; + my $new_y = $p->[1] + $dy->[$i]; + my $val = $v + 1; + if ($new_x >= 0 && $new_x <= 7 && + $new_y >= 0 && $new_y <= 7 && + not $visited->[$new_x]->[$new_y]) { + $visited->[$new_x]->[$new_y] = 1; + push @queue, [$new_x, $new_y, $val]; + } + } + } + return undef; +} + +for (@$cases) { + is(knights_move($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-281/peter-meszaros/tcl/ch-1.tcl b/challenge-281/peter-meszaros/tcl/ch-1.tcl new file mode 100644 index 0000000000..29f6ff02ea --- /dev/null +++ b/challenge-281/peter-meszaros/tcl/ch-1.tcl @@ -0,0 +1,60 @@ +#!/usr/bin/env tclsh +# +# Task 1: Check Color +# +# Submitted by: Mohammad Sajid Anwar +# +# You are given coordinates, a string that represents the coordinates of a square +# of the chessboard as shown below: +# +# +-+-+-+-+-+-+-+-+ +# 8 | |#| |#| |#| |#| +# 7 |#| |#| |#| |#| | +# 6 | |#| |#| |#| |#| +# 5 |#| |#| |#| |#| | +# 4 | |#| |#| |#| |#| +# 3 |#| |#| |#| |#| | +# 2 | |#| |#| |#| |#| +# 1 |#| |#| |#| |#| | +# +-+-+-+-+-+-+-+-+ +# a b c d e f g h +# +# Write a script to return true if the square is light, and false if the square +# is dark. +# +# Example 1 +# +# Input: $coordinates = "d3" +# Output: true +# +# Example 2 +# +# Input: $coordinates = "g5" +# Output: false +# +# Example 3 +# +# Input: $coordinates = "e6" +# Output: true + +package require tcltest + +set cases { + {d3 1 "Example 1"} + {g5 0 "Example 2"} + {e6 1 "Example 3"} +} + +proc check_color {pos} { + set l [split $pos {}] + return [expr ([scan [lindex $l 0] %c] + [lindex $l 1]) % 2] +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + check_color [lindex $case 0] + } [lindex $case 1] +} + +exit 0 diff --git a/challenge-281/peter-meszaros/tcl/ch-2.tcl b/challenge-281/peter-meszaros/tcl/ch-2.tcl new file mode 100644 index 0000000000..68f368cf9c --- /dev/null +++ b/challenge-281/peter-meszaros/tcl/ch-2.tcl @@ -0,0 +1,102 @@ +#!/usr/bin/env tclsh +# +# Task 2: Knight's Move +# +# Submitted by: Peter Campbell Smith +# +# A Knight in chess can move from its current position to any square two rows or +# columns plus one column or row away. So in the diagram below, if it starts a S, +# it can move to any of the squares marked E. +# +# Write a script which takes a starting position and an ending position and +# calculates the least number of moves required. +# +# +--+--+--+--+--+--+--+--+ +# 8 |a8| | | | | | | | +# 7 | | |E | |E | | | | +# 6 | |E | | | |E | | | +# 5 | | | |S | | | | | +# 4 | |E | | | |E | | | +# 3 | | |E | |E | | | | +# 2 | | | | | | |g2| | +# 1 | | | | | | | | | +# +--+--+--+--+--+--+--+--+ +# a b c d e f g h +# +# Example 1 +# +# Input: $start = 'g2', $end = 'a8' +# Ouput: 4 +# +# g2 -> e3 -> d5 -> c7 -> a8 +# +# Example 2 +# +# Input: $start = 'g2', $end = 'h2' +# Ouput: 3 +# +# g2 -> e3 -> f1 -> h2 +# + +package require tcltest +package require struct::queue +package require struct::matrix + +set cases { + {{g2 a8} 4 "Example 1"} + {{g2 h2} 3 "Example 2"} +} + +proc knights_move {params} { + + set s [split [lindex $params 0] {}] + set e [split [lindex $params 1] {}] + set sx [expr [scan [lindex $s 0] %c] - [scan "a" %c]] + set ex [expr [scan [lindex $e 0] %c] - [scan "a" %c]] + set sy [expr [lindex $s 1] - 1] + set ey [expr [lindex $e 1] - 1] + set dx {2 2 -2 -2 1 1 -1 -1} + set dy {1 -1 1 -1 2 -2 2 -2} + + set queue [::struct::queue] + $queue put [list $sx $sy 0] + + set visited [::struct::matrix] + $visited add rows 8 + $visited add columns 8 + + while {[$queue size]} { + set p [$queue get] + set x [lindex $p 0] + set y [lindex $p 1] + set v [lindex $p 2] + if {$x == $ex && $y == $ey} { + $queue destroy + $visited destroy + return $v + } + for {set i 0} {$i < 8} {incr i} { + set new_x [expr $x + [lindex $dx $i]] + set new_y [expr $y + [lindex $dy $i]] + set val [expr $v + 1] + if {$new_x >= 0 && $new_x <= 7 && + $new_y >= 0 && $new_y <= 7 && + [$visited get cell $new_x $new_y] != 1} { + $visited set cell $new_x $new_y 1 + $queue put [list $new_x $new_y $val] + } + } + } + $queue destroy + $visited destroy + return null +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + knights_move [lindex $case 0] + } [lindex $case 1] +} + +exit 0 diff --git a/challenge-281/ulrich-rieke/cpp/ch-1.cpp b/challenge-281/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..756a211ba5 --- /dev/null +++ b/challenge-281/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,43 @@ +#include <iostream>
+#include <string>
+#include <vector>
+
+using namespace std::string_literals ;
+
+std::vector<std::string> createLine( const std::string & start ) {
+ std::vector<std::string> line ;
+ for ( int i = 0 ; i < 8 ; i++ ) {
+ if ( i % 2 == 0 )
+ line.push_back( start ) ;
+ else {
+ if ( start == "light"s )
+ line.push_back( "dark"s ) ;
+ else
+ line.push_back( "light"s ) ;
+ }
+ }
+ return line ;
+}
+
+int main( ) {
+ std::cout << "Enter the coordinates of a field on a chess board!\n" ;
+ std::string field ;
+ std::cin >> field ;
+ std::string cols {"abcdefgh"} ;
+ std::string col { field.substr( 0 , 1 ) } ;
+ int colpos = cols.find( col ) ;
+ int row { std::stoi( field.substr( 1 , 1 ) ) } ;
+ std::vector<std::string> baseline { createLine( "dark"s ) } ;
+ bool result = false ;
+ if ( row == 1 ) {
+ result = (baseline[colpos] == "light"s) ;
+ std::cout << std::boolalpha << result << '\n' ;
+ }
+ else {
+ std::string foot { baseline[ colpos ] } ;
+ std::vector<std::string> col_line { createLine( foot ) } ;
+ result = (col_line[ row - 1 ] == "light"s) ;
+ std::cout << std::boolalpha << result << '\n' ;
+ }
+ return 0 ;
+}
diff --git a/challenge-281/ulrich-rieke/cpp/ch-2.cpp b/challenge-281/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..2af80967d2 --- /dev/null +++ b/challenge-281/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,80 @@ +#include <iostream>
+#include <vector>
+#include <string>
+#include <map>
+#include <sstream>
+#include <cstdlib>
+#include <algorithm>
+
+std::vector<std::string> split( const std::string & text , char delimiter ) {
+ std::istringstream istr { text } ;
+ std::string word ;
+ std::vector<std::string> words ;
+ while ( std::getline( istr , word , delimiter ) ) {
+ words.push_back( word ) ;
+ }
+ return words ;
+}
+
+//create all fields of a chess board
+std::vector<std::string> findAllFields( ) {
+ std::string cols {"abcdefgh"} ;
+ std::string rows {"12345678"} ;
+ std::vector<std::string> board ;
+ for ( int i = 0 ; i < cols.length( ) ; i++ ) {
+ for ( int j = 0 ; j < rows.length( ) ; j++ ) {
+ std::string field { cols.substr( i , 1 ).append( rows.substr( j , 1 ) ) } ;
+ board.push_back( field ) ;
+ }
+ }
+ return board ;
+}
+
+//all suitable fields on a chessboard are those that differ from the given field
+// by an absolute difference of 2 in rows and of 1 in columns or vice versa
+std::vector<std::string> findTargets( const std::string & field , const
+ std::vector<std::string> & board ) {
+ std::vector<std::string> selected ;
+ static const std::string columns {"abcdefgh"} ;
+ static const std::string rows {"12345678"} ;
+ auto selector = [field]( const auto & f ) {
+ auto colfound = columns.find_first_of( field.substr( 0 , 1 ) ) ;
+ auto rowfound = rows.find_first_of( field.substr( 1 , 1 ) ) ;
+ auto fieldcol = columns.find_first_of( f.substr( 0 , 1 ) ) ;
+ auto fieldrow = rows.find_first_of( f.substr( 1 , 1 ) ) ;
+ auto coldiff = abs( colfound - fieldcol ) ;
+ auto rowdiff = abs( rowfound - fieldrow ) ;
+ return ( (coldiff == 1) && (rowdiff == 2)) || ( (coldiff == 2) &&
+ (rowdiff == 1 )) ;
+ } ;
+ std::copy_if( board.begin( ) , board.end( ) , std::back_inserter( selected ) ,
+ selector ) ;
+ return selected ;
+}
+
+int main( ) {
+ std::cout << "Enter a source and a target field!\n" ;
+ std::string line ;
+ std::getline( std::cin, line ) ;
+ std::vector<std::string> fields { split( line , ' ' ) } ;
+ std::vector<std::string> chessboard { findAllFields( ) } ;
+ std::string sourceField { fields[ 0 ] } ;
+ std::string targetField { fields[ 1 ] } ;
+ std::vector<std::string> targets = findTargets( sourceField , chessboard ) ;
+ int count = 1 ;
+ while ( std::find( targets.begin( ) , targets.end( ) , targetField ) ==
+ targets.end( ) ) {
+ count++ ;
+ std::vector<std::string> newTargets , elementTargets ;
+ for ( auto it = targets.begin( ) ; it != targets.end( ) ; it++ ) {
+ elementTargets = findTargets( *it , chessboard ) ;
+ for ( auto f : elementTargets )
+ newTargets.push_back( f ) ;
+ elementTargets.clear( ) ;
+ }
+ for ( auto f : newTargets )
+ targets.push_back( f ) ;
+ }
+ std::cout << count << '\n' ;
+ return 0 ;
+}
diff --git a/challenge-281/ulrich-rieke/haskell/ch-1.hs b/challenge-281/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..ed267a395d --- /dev/null +++ b/challenge-281/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,25 @@ +module Challenge281
+ where
+import Data.List ( findIndices , ( !! ))
+import Data.Char ( digitToInt )
+
+createLine :: String -> [String]
+createLine base = if base == "light" then take 8 $ cycle ["light" , "dark"]
+ else take 8 $ cycle ["dark" , "light"]
+
+solution :: String -> Bool
+solution field =
+ let baseline = createLine "dark"
+ columns = "abcdefgh"
+ row = digitToInt $ last field
+ col = head field
+ colindex = head $ findIndices ( == col ) columns
+ in if row == 1 then (baseline !! colindex) == "light" else ( createLine
+ ( baseline !! colindex ) !! ( row - 1 ) ) == "light"
+
+
+main :: IO ( )
+main = do
+ putStrLn "Enter the coordinates of a chess field!"
+ field <- getLine
+ print $ solution field
diff --git a/challenge-281/ulrich-rieke/haskell/ch-2.hs b/challenge-281/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..85b360b52d --- /dev/null +++ b/challenge-281/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,40 @@ +module Challenge281_2
+ where
+import Control.Applicative
+import Data.Char ( digitToInt )
+import Data.Maybe ( fromJust )
+
+chessboard :: [(Char , Int)]
+chessboard = (,) <$> ['a'..'h'] <*> [1..8]
+
+convert :: String -> (Char , Int)
+convert field = ( head field , digitToInt $ last field )
+
+findSuitableTargets :: String -> [(Char , Int)]
+findSuitableTargets field =
+ let converted = convert field
+ indexedCols = zip ['a'..'h'] [1 , 2 ..]
+ fieldCol = fromJust $ lookup ( fst converted ) indexedCols
+ fieldRow = snd converted
+ in filter (\p -> ((abs ( (fromJust $ lookup ( fst p ) indexedCols) - fieldCol ) == 2 )
+ && ( abs ( fieldRow - snd p ) == 1 )) || (( abs ( (fromJust $ lookup ( fst p )
+ indexedCols) - fieldCol ) == 1 ) && ( abs ( fieldRow - snd p ) == 2 ))) chessboard
+
+solution :: String -> String -> Int
+solution from to = fst $ until (\p -> elem target ( snd p )) step (1 , findSuitableTargets
+ from )
+ where
+ target :: (Char , Int)
+ target = convert to
+ toString :: (Char , Int) -> String
+ toString ( c , num ) = [c] ++ show num
+ step :: (Int , [(Char , Int)]) -> (Int , [(Char , Int)] )
+ step ( count , associations ) = ( count + 1 , concat $ map (\p -> findSuitableTargets
+ ( toString p ) ) associations )
+
+main :: IO ( )
+main = do
+ putStrLn "Enter a source and a target chess field, separated by blanks!"
+ line <- getLine
+ let [from , to] = words line
+ print $ solution from to
diff --git a/challenge-281/ulrich-rieke/perl/ch-1.pl b/challenge-281/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..3ecc24160f --- /dev/null +++ b/challenge-281/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,52 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+sub createLine {
+ my $start = shift ;
+ my @line ;
+ for my $pos ( 0..7) {
+ if ( $pos % 2 == 0 ) {
+ push( @line, $start ) ;
+ }
+ else {
+ if ( $start eq "light" ) {
+ push( @line, "dark" ) ;
+ }
+ else {
+ push( @line , "light" ) ;
+ }
+ }
+ }
+ return @line ;
+}
+
+say "Enter a field on a chessboard!" ;
+my $field = <STDIN> ;
+chomp $field ;
+my $col = substr( $field, 0 , 1 ) ;
+my $row = substr( $field , 1 , 1 ) ;
+my $cols = "abcdefgh" ;
+my @baseline = createLine( "dark" ) ;
+my $result ;
+if ( $row == 1 ) {
+ if ( $baseline[ index( $cols , $col ) ] eq "light" ) {
+ $result = "true" ;
+ }
+ else {
+ $result = "false" ;
+ }
+}
+else {
+ my $foot = $baseline[ index( $cols , $col ) ] ;
+ my @col_line = createLine( $foot ) ;
+ if ( $col_line[ $row - 1 ] eq "light" ) {
+ $result = "true" ;
+ }
+ else {
+ $result = "false" ;
+ }
+}
+say $result ;
+
diff --git a/challenge-281/ulrich-rieke/perl/ch-2.pl b/challenge-281/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..29210ed3d1 --- /dev/null +++ b/challenge-281/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,54 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+#find the target fields of the given knight field! Keep doing so for every target
+#field until the target field we look for is in the array!
+
+#find possible target fields out of all chess fields possible!
+sub findTargetFields {
+ my $field = shift ;
+ my $chessfields = shift ;
+ my $cols = "abcdefgh" ;
+ my $rows = "12345678" ;
+ my @targetFields = grep { (abs( index( $cols , substr( $field , 0 , 1 )) -
+ index( $cols , substr( $_ , 0 , 1 ) ) ) == 1 && ( abs( index( $rows ,
+ substr( $field , 1 , 1 ) ) - index( $rows , substr( $_ , 1 , 1 ) ))
+ == 2 )) || ( abs( index( $cols , substr( $field , 0 , 1 ) ) - index( $cols ,
+ substr($_ , 0 , 1 ))) == 2 && ( abs( index( $rows , substr(
+ $field , 1 , 1 ) ) - index( $rows , substr( $_ , 1 , 1 ) ))
+ == 1 )) } @$chessfields ;
+ return @targetFields ;
+}
+
+say "Enter a start and a target field of a knight!" ;
+my $line = <STDIN> ;
+chomp $line ;
+(my $start , my $target ) = split( /\s+/ , $line ) ;
+#create all possible chess fields ;
+my $cols = "abcdefgh" ;
+my $rows = "12345678" ;
+my @chessfields ;
+for my $col( split( // , $cols ) ) {
+ for my $row ( split( // , $rows ) ) {
+ push( @chessfields , $col . $row ) ;
+ }
+}
+my %targetsFound ;
+my $count = 1 ;
+my @targetFields = findTargetFields( $start , \@chessfields ) ;
+map { $targetsFound{$_}++ } @targetFields ;
+while ( not ( exists( $targetsFound{ $target } ) ) ) {
+ $count++ ;
+ my @newTargets ;
+ for my $field ( keys %targetsFound ) {
+ my @currentTargets = findTargetFields( $field , \@chessfields ) ;
+ for my $el ( @currentTargets ) {
+ push( @newTargets , $el ) ;
+ }
+ }
+ map { $targetsFound{ $_}++ } @newTargets ;
+ @newTargets = ( ) ;
+}
+say $count ;
diff --git a/challenge-281/ulrich-rieke/raku/ch-1.raku b/challenge-281/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..f24c61aef5 --- /dev/null +++ b/challenge-281/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,35 @@ +use v6 ;
+
+sub createLine( $start ) {
+ my @line ;
+ for (0..7) -> $pos {
+ if ( $pos %% 2 ) {
+ @line.push( $start ) ;
+ }
+ else {
+ if ( $start eq "light" ) {
+ @line.push( "dark" ) ;
+ }
+ else {
+ @line.push( "light" ) ;
+ }
+ }
+ }
+ return @line ;
+}
+
+say "Enter the coordinates of a chess field!" ;
+my $field = $*IN.get ;
+my $cols = "abcdefgh" ;
+my $col = $field.substr( 0 , 1 ) ;
+my $row = $field.substr( 1 , 1 ).map( {.Int} ) ;
+my @baseline = createLine( "dark" ) ;
+my $result ;
+if ( $row == 1 ) {
+ $result = @baseline[ $cols.index( $col ) ] eq "light" ;
+}
+else {
+ my @col_line = createLine( @baseline[ $cols.index( $col )] ) ;
+ $result = @col_line[ $row - 1 ] eq "light" ;
+}
+say $result ;
diff --git a/challenge-281/ulrich-rieke/raku/ch-2.raku b/challenge-281/ulrich-rieke/raku/ch-2.raku new file mode 100755 index 0000000000..0e3e46e3cf --- /dev/null +++ b/challenge-281/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,47 @@ +use v6 ;
+
+#the basic idea is : find all possible target fields for a given source field!
+#then, for every target field found , again create all new target fields until
+#the initial target field is in it
+
+sub findTargets( @allFields , $start ) {
+ my $cols = "abcdefgh" ;
+ my $rows = "12345678" ;
+ my @targets = @allFields.grep( { abs($cols.index( $start.substr( 0 , 1 )) -
+ $cols.index($_.substr( 0 , 1))) == 1 && abs( $rows.index( $start.substr(1 , 1 )) -
+ $rows.index( $_.substr( 1 , 1 ) )) == 2 || ( abs( $cols.index(
+ $start.substr( 0 , 1 )) - $cols.index( $_.substr( 0 , 1 ))) == 2 &&
+ (abs( $rows.index( $start.substr( 1 , 1 ) ) - $rows.index( $_.substr( 1 , 1 )))
+ == 1 ) ) } ) ;
+ return @targets ;
+}
+
+say "Enter a start and a target chess field!" ;
+my $line = $*IN.get ;
+(my $start , my $target ) = $line.words ;
+my $cols = "abcdefgh" ;
+my $rows = "12345678" ;
+my @chessFields ;
+for ($cols.comb) -> $column {
+ for ( $rows.comb) -> $row {
+ my $field = $column ~ $row ;
+ @chessFields.push( $field ) ;
+ }
+}
+my $count = 1 ;
+my @newTargets = findTargets( @chessFields , $start ) ;
+my $fieldSet = @newTargets.Set ;
+while ( not ( $target (elem) $fieldSet ) ) {
+ $count++ ;
+ my @nextTargets ;
+ for @newTargets -> $tar {
+ my @elementsTargets = findTargets( @chessFields , $tar ) ;
+ for (@elementsTargets) -> $el {
+ @nextTargets.push( $el ) ;
+ }
+ @elementsTargets = ( ) ;
+ }
+ @newTargets = @nextTargets ;
+ $fieldSet = @newTargets.Set ;
+}
+say $count ;
diff --git a/challenge-281/ulrich-rieke/rust/ch-1.rs b/challenge-281/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..3f83868afd --- /dev/null +++ b/challenge-281/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,35 @@ +use std::io ; + +fn main() { + println!("Enter a chess field!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let entered_line : &str = inline.as_str( ).trim( ) ; + let cols : &str = "abcdefgh" ; + let col : char = entered_line.chars( ).nth( 0 ).unwrap( ) ; + let row : u32 = entered_line.chars( ).nth( 1 ).unwrap( ).to_digit( 10 ).unwrap( ) ; + let first_pair : Vec<&str> = vec!["light" , "dark"] ; + let second_pair : Vec<&str> = vec!["dark" , "light"] ; + let colnum = cols.find( col ).unwrap( ) ; + let mut baseline_colors : Vec<&str> = Vec::new( ) ; + let it = second_pair.iter( ).cycle( ).take( 8 ) ; + it.for_each( | f | baseline_colors.push( f ) ) ; + let result : bool ; + if row == 1 { + result = baseline_colors.iter( ).nth( colnum ).unwrap( ) == &"light" ; + } + else { + let base_color : &str = baseline_colors[colnum] ; + let mut col_sequence : Vec<&str> = Vec::new( ) ; + if base_color == "light" { + let sec_it = first_pair.iter( ).cycle( ).take( 8 ) ; + sec_it.for_each( | f | col_sequence.push( f ) ) ; + } + else { + let it |
