aboutsummaryrefslogtreecommitdiff
path: root/challenge-190/ulrich-rieke/cpp/ch-2.cpp
blob: 16b1055105ae4bcfb863b7273af11c3431192511 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

//find all combinations of 1 and 2 that add up to the given length
std::vector<std::vector<int>> findAllCombinations( int len ) {
  std::vector<std::vector<int>> allCombinations ;
  std::vector<int> currentCombi ;
  std::vector<std::vector<int>> thePermus ;
  for ( int twos = 0 ; twos < len / 2 + 1 ; twos++ ) {
      int ones = len - twos * 2 ;
      for ( int i = 0 ; i < ones ; i++ ) {
    currentCombi.push_back( 1 ) ;
      }
      for ( int i = 0 ; i < twos ; i++ ) {
    currentCombi.push_back( 2 ) ;
      }
      allCombinations.push_back( currentCombi ) ;
      currentCombi.clear( ) ;
  }
  //permute the combinations that contain both 1 and 2
  for ( auto combi : allCombinations ) {
      if ( std::find( combi.begin( ) , combi.end( ) , 1 ) != combi.end( )
        && ( std::find( combi.begin( ) , combi.end( ) , 2 ) !=
          combi.end( ))) {
    std::vector<int> aCombi { combi } ;
    while (std::next_permutation( aCombi.begin( ) , aCombi.end( ) )) {
        thePermus.push_back( aCombi ) ;
    }
      }
  }
  //push the permutations to the existing combinations
  for ( auto combi : thePermus ) {
      allCombinations.push_back( combi ) ;
  }
  return allCombinations ;
}

//take the given number string apart according to the combination of
//1's an 2's
std::vector<int> findNumbers( const std::string & st , const
      std::vector<int> & nums ) {
  std::vector<int> numbers ;
  int pos = 0 ;
  for ( int i : nums ) {
      numbers.push_back( std::stoi( st.substr( pos , i ))) ;
      pos += i ;
  }
  return numbers ;
}

//transform every number into a letter
std::string createWord( const std::vector<int> & numbers ) {
  std::string word ;
  if ( std::any_of( numbers.begin( ) , numbers.end( ) , []( int i ) {
        return i > 26 ; })) {
      return "" ;
  }
  else {
      for ( int n : numbers ) {
    word += static_cast<char>( n + 64 ) ;
      }
      return word ;
  }
}

int main( ) {
  std::cout << "Please enter a string consisting only of digits!\n" ;
  std::string line ;
  std::cin >> line ;
  std::vector<std::string> allWords ;
  std::vector<std::vector<int>> allCombinations ( findAllCombinations(
        line.length( ))) ;
  for ( auto & comb : allCombinations ) {
      std::string word ( createWord( findNumbers( line , comb ))) ;
      if ( word.length( ) > 0 )
    allWords.push_back( word ) ;
  }
  std::sort( allWords.begin( ) , allWords.end( ) ) ;
  for ( const auto & w : allWords ) {
      std::cout << w << ' ' ;
  }
  std::cout << std::endl ;
  return 0 ;
}