From c34bb5d7bd7fce08e8311a0f527ce7fbd69e4dae Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 26 Jan 2021 17:36:17 +0000 Subject: - Added solutions by Ulrich Rieke. --- challenge-097/ulrich-rieke/cpp/ch-2.cpp | 64 +++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 challenge-097/ulrich-rieke/cpp/ch-2.cpp (limited to 'challenge-097/ulrich-rieke/cpp/ch-2.cpp') 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 +#include +#include +#include +#include +#include +#include + +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 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 !" ; + 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 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 alterations ; + for ( std::string & word : words ) { + alterations.push_back( countToMakeAllEqual( word ) ) ; + } + std::cout << std::accumulate( alterations.begin( ) , alterations.end( ) , 0 ) + << std::endl ; + return 0 ; +} -- cgit