diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-06-28 00:53:23 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-06-28 00:53:23 +0100 |
| commit | 4c358ffd5e0018166c990bbde73a816b854c3a20 (patch) | |
| tree | 582dad725b12c09c154f309efd518e7fcbbd076e /challenge-118 | |
| parent | 4622f1ecc474a0102c479a56faa701914b8b6f6c (diff) | |
| download | perlweeklychallenge-club-4c358ffd5e0018166c990bbde73a816b854c3a20.tar.gz perlweeklychallenge-club-4c358ffd5e0018166c990bbde73a816b854c3a20.tar.bz2 perlweeklychallenge-club-4c358ffd5e0018166c990bbde73a816b854c3a20.zip | |
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-118')
| -rw-r--r-- | challenge-118/ulrich-rieke/cpp/ch-1.cpp | 21 | ||||
| -rw-r--r-- | challenge-118/ulrich-rieke/cpp/ch-2.cpp | 93 | ||||
| -rw-r--r-- | challenge-118/ulrich-rieke/haskell/ch-1.hs | 25 | ||||
| -rw-r--r-- | challenge-118/ulrich-rieke/lisp/ch-1.lisp | 12 | ||||
| -rw-r--r-- | challenge-118/ulrich-rieke/perl/ch-1.pl | 17 | ||||
| -rw-r--r-- | challenge-118/ulrich-rieke/perl/ch-2.pl | 83 | ||||
| -rw-r--r-- | challenge-118/ulrich-rieke/raku/ch-1.raku | 11 |
7 files changed, 262 insertions, 0 deletions
diff --git a/challenge-118/ulrich-rieke/cpp/ch-1.cpp b/challenge-118/ulrich-rieke/cpp/ch-1.cpp new file mode 100644 index 0000000000..4b6af4a7d7 --- /dev/null +++ b/challenge-118/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,21 @@ +#include <cstdlib> +#include <iostream> +#include <string> +#include <algorithm> + +int main( int argc, char * argv[] ) { + int n = std::atoi( argv[ 1 ] ) ; + std::string binarystring ; + while ( n != 0 ) { + binarystring.append( std::to_string( n % 2 ) ) ; + n /= 2 ; + } + std::reverse( binarystring.begin( ) , binarystring.end( ) ) ; + std::string reversed( binarystring ) ; + std::reverse( reversed.begin( ) , reversed.end( ) ) ; + if ( reversed == binarystring ) + std::cout << 1 << std::endl ; + else + std::cout << 0 << std::endl ; + return 0 ; +} diff --git a/challenge-118/ulrich-rieke/cpp/ch-2.cpp b/challenge-118/ulrich-rieke/cpp/ch-2.cpp new file mode 100644 index 0000000000..0fe558a7cf --- /dev/null +++ b/challenge-118/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,93 @@ +#include <utility> +#include <list> +#include <array> +#include <vector> +#include <map> +#include <iostream> + +//compute the fields the knights can reach from a given position +std::vector<std::pair<int, int>> findTargetFields( const std::pair<int, int> + & p ) { + std::pair<int, int> neighbour( p ) ; + std::vector<std::pair<int , int>> neighbours ; + static std::array<int , 8> xmoves {2 , 1 , 1 , -2 , -1 , 2 , -1 , -2 } ; + static std::array<int , 8> ymoves { 1 , 2 , -2 , 1 , 2 , -1 , -2 , -1 } ; + for ( int i = 0 ; i < 8 ; i++ ) { + neighbour.first += xmoves[ i ] ; + neighbour.second += ymoves[ i ] ; + neighbours.push_back( neighbour ) ; + neighbour = p ;//put back to start + } + return neighbours ; +} + +//is the position on board +bool isValid( const std::pair<int, int> & cell ) { + return ( cell.first > 0 && cell.first < 9 && cell.second > 0 && + cell.second < 9 ) ; +} + +//move from one point to the next +//we store all valid points on the board in allPoints and keep popping values +//from the front until we have the target point +//for every point, we have a given path which we store in a map with the point +//as key. +//Before we pop a value from the top of allPoints, we take the front point as +//the new currentPoint and the corresponding path for this point as the new path +std::list<std::pair<int , int>> moveFromPointToPoint( const std::pair<int, int> + & startPoint , const std::pair<int, int> & targetPoint ) { + std::list<std::pair<int , int>> path ; + path.push_back( startPoint ) ; + std::pair<int , int> currentPoint( startPoint ) ; + std::map<std::pair<int , int> , std::list<std::pair<int, int>>> pathsForPoints ; + pathsForPoints[ startPoint ] = path ; + std::list<std::pair<int , int>> allPoints { startPoint } ; + std::array<std::array<bool , 8> , 8> visited ; + std::array<bool , 8> row ; + row.fill( false ) ; + visited.fill( row ) ; + visited[startPoint.first - 1][startPoint.second - 1] = true ; + while ( currentPoint != targetPoint ) { + allPoints.pop_front( ) ; + std::vector<std::pair<int , int>> targetFields = findTargetFields( + currentPoint ) ; + for ( auto & p : targetFields ) { + if ( isValid( p ) && ! visited[p.first - 1][p.second - 1] ) { + allPoints.push_back( p ) ; + path.push_back( p ) ; + pathsForPoints[ p ] = path ; + visited[ p.first -1][p.second - 1] = true ; + path = pathsForPoints[ currentPoint ] ;//return to the path for + //currentPoint ! + } + } + currentPoint = allPoints.front( ) ; + path = pathsForPoints[ currentPoint ] ; + } + return pathsForPoints[ currentPoint ] ; +} + +int main( ) { + std::vector<std::pair<int , int>> treasures { {6 , 5} , {4 , 3 } , {3 , 2 } , + {2 , 1 } , {2 , 2 } , {1 , 2 }} ; + std::pair<int , int> knightPos { 8 , 1 } ; + std::list<std::pair<int , int>> totalPath = moveFromPointToPoint( knightPos , + *(treasures.begin( ) ) ) ; + int len = treasures.size( ) ; + for ( int i = 0 ; i < len - 1 ; i++ ) { + std::list<std::pair<int, int>> path = moveFromPointToPoint( + *(treasures.begin( ) + i ) , *(treasures.begin( ) + i + 1 )) ; + path.pop_front( ) ; //the starting point was the end point of previous move! + for ( auto & p : path ) { + totalPath.push_back( p ) ; + } + } + for ( auto & p : totalPath ) { + std::cout << '(' << p.first << ',' << p.second << ')' ; + if ( p != totalPath.back( ) ) + std::cout << "->" ; + else + std::cout << std::endl ; + } + return 0 ; +} diff --git a/challenge-118/ulrich-rieke/haskell/ch-1.hs b/challenge-118/ulrich-rieke/haskell/ch-1.hs new file mode 100644 index 0000000000..c0714039e4 --- /dev/null +++ b/challenge-118/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,25 @@ +module Challenge118 + where +import Control.Monad.State.Lazy +import Data.Char ( intToDigit ) + +toBase :: State ([Integer] , Integer , Integer ) [Integer] +toBase = do + ( basenumbers , num , base ) <- get + if num == 0 + then + return basenumbers + else do + put ( mod num base : basenumbers , div num base , base ) + toBase + +baseNumbers :: Integer -> Integer -> [Integer] +baseNumbers n b = evalState toBase ( [] , n , b ) + +solution :: Integer -> Int +solution n + |reverse binarystring == binarystring = 1 + |otherwise = 0 + where + binarystring :: String + binarystring = map ( intToDigit . fromInteger ) $ evalState toBase ([] , n , 2 ) diff --git a/challenge-118/ulrich-rieke/lisp/ch-1.lisp b/challenge-118/ulrich-rieke/lisp/ch-1.lisp new file mode 100644 index 0000000000..23f8833ffe --- /dev/null +++ b/challenge-118/ulrich-rieke/lisp/ch-1.lisp @@ -0,0 +1,12 @@ +#!/usr/bin/sbcl --script + +(defun solution ( n ) +(let (( numberstring ( format nil "~b" n )) + ( reversed )) + (setq reversed ( reverse numberstring )) + (if (string= numberstring reversed) + 1 + 0))) + +(format t "~d~%" (solution 4)) +(format t "~d~%" (solution 5 )) diff --git a/challenge-118/ulrich-rieke/perl/ch-1.pl b/challenge-118/ulrich-rieke/perl/ch-1.pl new file mode 100644 index 0000000000..b87d27930c --- /dev/null +++ b/challenge-118/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +my $N = $ARGV[0] ; +my @bits ; +while ( $N != 0 ) { + unshift( @bits , $N % 2 ) ; + $N = int( $N / 2 ) ; +} +if ( join( '' , @bits ) ne scalar ( reverse @bits )) { + say 0 ; +} +else { + say 1 ; +} diff --git a/challenge-118/ulrich-rieke/perl/ch-2.pl b/challenge-118/ulrich-rieke/perl/ch-2.pl new file mode 100644 index 0000000000..3c366d87fe --- /dev/null +++ b/challenge-118/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,83 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +#check whether a given field is on board +sub isOnBoard { + my $pos = shift ; + if ( ($pos->[0] > 0 && $pos->[0] < 9 ) && ( $pos->[1] > 0 and + $pos->[1] < 9 ) ) { + return 1 ; + } + return 0 ; +} + +#start and target positions are given as an array reference +#for every field we create two hashes : %visited which checks whether +#the given field was already visited +#and %pathForField, which stores the path to the given field +sub moveFromFieldToField { + my $startPos = shift ; + my $targetPos = shift ; + my @xmoves = ( -2 , -1 , 1 , 2 , -2 , -1 , 1 , 2 ) ; + my @ymoves = ( -1 , -2 , -2 , -1 , 1 , 2 , 2 , 1 ) ; + my @path = ( $startPos ) ; #we begin the path with the start position + my @allFields = ( $startPos ) ;#we put all valid fields into this array + my %pathForField ; + my %visited ; + for my $i (1 .. 8 ) { #we initiate %visited with 0 ; when visited it's set to 1 + for my $j (1 .. 8 ) { + $visited{ "$i$j" } = 0 ; + } + } + $pathForField{ join( '' , @$startPos ) } = \@path ; + my $currentPos ; #for the current position of the knight + $visited{ join( '' , @$startPos ) } = 1 ; # start position is visited + while ( @allFields ) { #we shift the fields 1 by 1 from this array to see if we +#are at the target already + my $topPos = shift @allFields ; + my @top = @$topPos ; #for the sake of code reliability dereference array refer. +#our new path is the path for the field that we have just shifted from the @allFields +#array + @path = @{$pathForField{ join( '' , @top ) }} ; + if ( ($topPos->[0] == $targetPos->[0]) && ( $topPos->[1] #are we there ? + == $targetPos->[1] ) ) { +#the path for the target position is found in the %pathForField hash +#for heaven's sake, dereference the array reference that is stored as value +#in the hash, with the field being the key. I was in a lot of trouble while I +#didn't do it.. + my @thisPath = @{$pathForField{ join( '' , @$topPos )}} ; + return @thisPath ; + } +#starting with the top position from @allFields we do all legal knight moves + for my $i (0 .. 7 ) { + $currentPos->[0] = $topPos->[0] ; + $currentPos->[1] = $topPos->[1] ; + $currentPos->[0] += $xmoves[ $i ] ; + $currentPos->[1] += $ymoves[ $i ] ; + if ( (isOnBoard( $currentPos )) && + ($visited{join( '' , @$currentPos)} == 0) ) { + my @boardarray = @$currentPos ; #dereference!! + my @newpath = @path ; #dereference the old path + push @newpath , \@boardarray ;#and add the new legal field on board + $pathForField{ join( '' , @boardarray) } = \@newpath ;#store it + push @allFields , \@boardarray ; #store in the fields array + $visited{ join( '' , @boardarray ) } = 1 ;#and mark as visited + } + } + } +} +my @treasures = ( [6 , 5] , [4 , 3] , [3 , 2 ] , + [2 , 1] , [2, 2] , [1 , 2] ) ; +my @path ;#for the way from left upper corner to all treasure fields +my @way = moveFromFieldToField( [8,1] , $treasures[ 0 ] ) ; +push @path , @way ; +my $len = scalar @treasures ; +for my $i (0 .. $len - 2 ) { + @way = moveFromFieldToField( $treasures[ $i ] , $treasures[ $i + 1 ] ) ; + push @path , @way[1..$#way] ;#we leave out the index 0 of @way since the starting +#point of move n + 1 was the end point of move n +} +my @iter = map { "(" . join ( ',' , @$_ ) . ")" } @path ; +say join( '->' , @iter ) ; diff --git a/challenge-118/ulrich-rieke/raku/ch-1.raku b/challenge-118/ulrich-rieke/raku/ch-1.raku new file mode 100644 index 0000000000..2a0740cba2 --- /dev/null +++ b/challenge-118/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,11 @@ +use v6 ; + +sub MAIN( Int $N is copy ) { + my $binaryString = $N.base( 2 ) ; + if ( $binaryString eq $binaryString.flip ) { + say 1 ; + } + else { + say 0 ; + } +} |
