diff options
Diffstat (limited to 'challenge-097')
| -rw-r--r-- | challenge-097/ulrich-rieke/cpp/ch-1.cpp | 35 | ||||
| -rw-r--r-- | challenge-097/ulrich-rieke/cpp/ch-2.cpp | 64 | ||||
| -rw-r--r-- | challenge-097/ulrich-rieke/haskell/ch-1.hs | 17 | ||||
| -rw-r--r-- | challenge-097/ulrich-rieke/haskell/ch-2.hs | 27 | ||||
| -rw-r--r-- | challenge-097/ulrich-rieke/perl/ch-1.pl | 25 | ||||
| -rw-r--r-- | challenge-097/ulrich-rieke/perl/ch-2.pl | 43 | ||||
| -rw-r--r-- | challenge-097/ulrich-rieke/raku/ch-1.raku | 12 | ||||
| -rw-r--r-- | challenge-097/ulrich-rieke/raku/ch-2.raku | 52 |
8 files changed, 275 insertions, 0 deletions
diff --git a/challenge-097/ulrich-rieke/cpp/ch-1.cpp b/challenge-097/ulrich-rieke/cpp/ch-1.cpp new file mode 100644 index 0000000000..d69135b4cc --- /dev/null +++ b/challenge-097/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,35 @@ +#include <iostream> +#include <cstdlib> +#include <string> +#include <algorithm> +#include <iterator> + +char findCipher( char c, int shift ) { + static const std::string alphabet {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"} ; + std::string mapped { alphabet.substr(26 - shift) } ; + std::reverse ( mapped.begin( ) , mapped.end( ) ) ; + mapped.append( alphabet.substr(0 , 26 - shift ) ) ; + if ( c == ' ' ) + return ' ' ; + else { + auto found = std::find( alphabet.begin( ) , alphabet.end( ) , c ); + return mapped[ static_cast<int>(std::distance(alphabet.begin( ), found))] ; + } +} + +int main( int i , char* argv[ ] ) { + if ( i != 3 ) { + std::cerr << "usage: challenge097 <capital letter string> <leftshift>!\n" ; + return 1 ; + } + int leftshift = std::atoi( argv[ 2 ] ) ; + int realshift = leftshift % 26 ; + std::string plaintext( argv[ 1 ] ) ; + int len = plaintext.length( ) ; + std::string ciphertext ; + for ( char letter : plaintext ) { + ciphertext.push_back( findCipher ( letter , realshift ) ) ; + } + std::cout << ciphertext << std::endl ; + return 0 ; +} diff --git a/challenge-097/ulrich-rieke/cpp/ch-2.cpp b/challenge-097/ulrich-rieke/cpp/ch-2.cpp new file mode 100644 index 0000000000..7fa22ce546 --- /dev/null +++ b/challenge-097/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,64 @@ +#include <iostream> +#include <string> +#include <vector> +#include <map> +#include <cstdlib> +#include <algorithm> +#include <numeric> + +bool isInputValid( const std::string & input , int n ) { + return input.length( ) % n == 0 ; +} + +//how many digits must be changed to make them all equal +int countToMakeAllEqual( const std::string & input ) { + std::map<std::string , int> frequencies ; + frequencies["0"] = 0 ; + frequencies["1"] = 0 ; + int len = input.length( ) ; + for ( int i = 0 ; i < len ; i++ ) { + frequencies[ input.substr( i , 1 )]++ ; + } + if ( frequencies[ "0" ] == len || frequencies[ "1" ] == len ) + return 0 ; + int bigger = std::max( frequencies["0"] , frequencies[ "1" ] ) ; + return len - bigger ; +} + +int main( int argc, char * argv[ ] ) { + if ( argc != 3 ) { + std::cerr << "There should be 2 arguments, call <challenge097_2> <string> <number>!" ; + return 1 ; + } + std::string input( argv[ 1 ] ) ; + int blocks { std::atoi( argv[ 2 ] ) } ; + if ( ! isInputValid( input , blocks ) ) { + std::cerr << "the number of digits in the binary string should be a multiple of " ; + std::cerr << blocks << " !" << std::endl ; + return 2 ; + } + int len = input.length( ) ; + int chunknumber = len / blocks ; + int chunklength { len / chunknumber } ; + std::vector<std::string> words ; + //for all blocks to be equal we transpose the blocks, that is the first letters + //of every block form a word, the second letters and so on + //we then see how many digits have to be flipped to make all digits equal + std::string transposed ; + for ( int i = 0 ; i < chunklength ; i++ ) { + for ( int j = 0 ; j < chunknumber ; j++ ) { + transposed.append( input.substr( i + j * chunklength , 1 ) ) ; + if ( transposed.length( ) == chunknumber ) { + words.push_back( transposed ) ; + transposed.clear( ) ; + } + } + } + std::vector<int> alterations ; + for ( std::string & word : words ) { + alterations.push_back( countToMakeAllEqual( word ) ) ; + } + std::cout << std::accumulate( alterations.begin( ) , alterations.end( ) , 0 ) + << std::endl ; + return 0 ; +} diff --git a/challenge-097/ulrich-rieke/haskell/ch-1.hs b/challenge-097/ulrich-rieke/haskell/ch-1.hs new file mode 100644 index 0000000000..a47e593861 --- /dev/null +++ b/challenge-097/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,17 @@ +module Challenge097 + where +import Data.Maybe( fromJust ) + +caesarencode :: String -> Int -> String +caesarencode plain leftshift = map (\c -> if c /= ' ' then fromJust $ lookup c +mappedPairs else ' ') plain +where + alfabet :: [Char] + alfabet = ['A' .. 'Z'] + realshift :: Int + realshift = mod leftshift ( length alfabet ) + leftRotated :: [Char] + leftRotated = (take realshift $ reverse alfabet) ++ take ( length alfabet - + realshift ) alfabet + mappedPairs :: [(Char , Char)] + mappedPairs = zip alfabet leftRotated diff --git a/challenge-097/ulrich-rieke/haskell/ch-2.hs b/challenge-097/ulrich-rieke/haskell/ch-2.hs new file mode 100644 index 0000000000..95adceaa76 --- /dev/null +++ b/challenge-097/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,27 @@ +module Challenge097_2 + where +import Data.List ( transpose ) +import qualified Data.Text as T + +solution :: String -> Int -> Int +solution binarystring blocks = + let strlen = length binarystring + chunknumber = div strlen blocks + chunksize = div strlen chunknumber + chunks = map T.unpack $ T.chunksOf chunksize $ T.pack binarystring + myWords = transpose chunks + toBeFlipped = map findFlips myWords + in sum toBeFlipped + +count :: Char -> String -> Int +count c str = length $ filter ( c == ) str + +findFlips :: String -> Int +findFlips str + |zeroes >= ones = length str - zeroes + |otherwise = length str - ones + where + zeroes :: Int + zeroes = count '0' str + ones :: Int + ones = count '1' str diff --git a/challenge-097/ulrich-rieke/perl/ch-1.pl b/challenge-097/ulrich-rieke/perl/ch-1.pl new file mode 100644 index 0000000000..52cf4e6646 --- /dev/null +++ b/challenge-097/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,25 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +sub findMapped { + my $letter = shift ; + my $n = shift ; + if ( $letter eq ' ' ) { + return ' ' ; + } + else { + my $num = ord( $letter ) - $n ; + if ( $num < 65 ) { #this is A, we must wrap to the end of the alphabet + $num = 90 - ( 65 - $num ) + 1 ; + } + return chr $num ; + } +} + +my $S = $ARGV[ 0 ] ; +my $N = $ARGV[ 1 ] ; +my $num = $N % 26 ; #if a number greater than the number of letters is ent. +die "String $S should only consist of capital letters" unless ($S =~ /^[A-Z ]+$/) ; +say join( '' , map { findMapped( $_ , $num ) } split( // , $S ) ) ; diff --git a/challenge-097/ulrich-rieke/perl/ch-2.pl b/challenge-097/ulrich-rieke/perl/ch-2.pl new file mode 100644 index 0000000000..68f94621cb --- /dev/null +++ b/challenge-097/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; +use List::Util qw( sum ) ; + +sub countToMakeAllEqual { + my $str = shift ; + my %frequencies ; + $frequencies{ '0' } = '0' ; + $frequencies{ '1' } = '0' ; + my $len = length( $str ) ; + for my $i ( '0' .. $len - '1' ) { + $frequencies{ substr( $str , $i , '1' ) }++ ; + } + if ( $frequencies{ '0' } == $len or $frequencies{ '1' } == $len ) { + return '0' ; + } + elsif ( $frequencies{ '0' } >= $frequencies{ '1' } ) { + return $len - $frequencies{ '0' } ; + } + else { + return $len - $frequencies{ '1' } ; + } +} + +my $B = $ARGV[ 0 ] ; +my $S = $ARGV[ 1 ] ; +die "The length of $B should be a multiple of $S" unless ( (length $B) % $S == 0 ) ; +my $len = length( $B ) ; +my $chunknumber = $len / $S ; +my $chunklength = $len / $chunknumber ; +my @chunks ; +for my $i ( 0 .. $chunknumber - 1 ) { + push (@chunks , substr( $B, $i * $chunklength , $chunklength ) ) ; +} +my @words ; +for my $i ( 0 .. $chunklength - 1 ) { + for my $j ( 0 .. $chunknumber - 1 ) { + $words[ $i ] .= substr( $chunks[ $j ] , $i , 1 ) ; + } +} +say sum map { countToMakeAllEqual( $_ ) } @words ; diff --git a/challenge-097/ulrich-rieke/raku/ch-1.raku b/challenge-097/ulrich-rieke/raku/ch-1.raku new file mode 100644 index 0000000000..b8aaaf027d --- /dev/null +++ b/challenge-097/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,12 @@ +use v6 ; + +sub MAIN( Str $S, Int $N ) { + my @alphabet = ('A' .. 'Z') ; + my @ciphers = @alphabet.rotate( -($N mod 26)) ; + my %correlations ; + %correlations{ ' ' } = ' ' ; + for (0 .. 25 ) -> $i { + %correlations{ @alphabet[ $i ] } = @ciphers[ $i ] ; + } + say $S.comb.map( { %correlations{ $_ } } ).join ; +} diff --git a/challenge-097/ulrich-rieke/raku/ch-2.raku b/challenge-097/ulrich-rieke/raku/ch-2.raku new file mode 100644 index 0000000000..3504352f18 --- /dev/null +++ b/challenge-097/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,52 @@ +use v6 ; + +sub isInputValid( Str $str , Int $n --> Bool ) { + return $str.chars %% $n ; +} + +#how many fields must be flipped to make all digits equal ? +#we zip the forward and reverse string and count how many pairs +#have different digits +sub countToMakeAllEqual( Str $str is copy --> Int ) { + my %frequencies ; + %frequencies<0> = 0 ; + %frequencies<1> = 0 ; + my $len = $str.chars ; + for ( 0 .. $len - 1 ) -> $i { + %frequencies{ $str.substr( $i , 1 ) }++ ; + } + if ( %frequencies<0> == $len or %frequencies<1> == $len ) { + return 0 ; + } + elsif ( %frequencies<0> >= %frequencies<1> ) { + return $len - %frequencies<0> ; + } + else { + return $len - %frequencies<1> ; + } +} + +sub MAIN( Str $B is copy, Int $S ) { + die "the length of $B should be a multiple of $S!" unless + isInputValid( $B , $S ) ; + my $len = $B.chars ; + my $chunknumber = $len div $S ; + my $chunklength = $len div $chunknumber ; +#we now transpose the chunks, that is the first letters of all chunks +#go into one word, the second letters into another and so on +#these transposed chunks should all consist of the same digits in the end +#word by word we sum up the flips that are necessary to make all digits +#equal +#first, we transpose + my @chunks ; + for (0 .. $chunknumber - 1 ) -> $i { + @chunks.push( $B.substr( $i * $chunklength , $chunklength ) ) ; + } + my @words ; + for (0 .. $chunklength - 1 ) -> $i { + for (0 .. $chunknumber - 1 ) -> $j { + @words[ $i ] ~= @chunks[ $j ].substr( $i , 1 ) ; + } + } + say @words.map( { countToMakeAllEqual( $_ ) } ).sum ; +} |
