-
Notifications
You must be signed in to change notification settings - Fork 0
/
24_solver.cpp
252 lines (234 loc) · 9.01 KB
/
24_solver.cpp
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <chrono>
#include <string>
using namespace std;
using namespace std::chrono;
/* variabel global*/
vector<string> listSol; // list yang menampung string solusi
bool valid = true; // false berarti masukan user salah
/***
* Mengembalikan angka berdasarkan masukan input
*/
int convertInput(string input){
int num;
if (input == "A"){
return 1;
} else if (input == "J"){
return 11;
} else if (input == "Q") {
return 12;
} else if (input == "K") {
return 13;
} else {
stringstream ss;
ss << input;
ss >> num;
if (num < 2 || num >10){
valid = false;
}
return num;
}
}
/***
* Mengembalikan hasil operasi dari : num1 op num2
*/
double operate(double num1, double num2, char op){
if (op == '+'){
return num1 + num2;
} else if (op == '-'){
return num1 - num2;
} else if (op == 'x'){
return num1 * num2;
} else {
return num1 / num2;
}
}
/***
* Mengembalikan string yang merupakan hasil konkatenasi : (num1 op num2)
*/
string concat(string num1, string num2, char op){
string res;
string opStr;
res = "("+ num1 +" " + opStr.append(1, op) + " " + num2 + ")";
return res;
}
/***
* Mengembalikan string hasil konversi num
*/
string convert(double num){
return to_string(int(num));
}
/**
* Menambahkan solution pada listSol jika solution belum ada di listSol
*/
void addSolution(string solution){
bool ada = false;
for (auto sol: listSol){
if (solution == sol){
ada= true;
}
}
if (!ada){
listSol.push_back(solution);
}
}
/***
* menambahkan semua solusi yang memenuhi dari 1 kombinasi masukan dan urutan operasi
*/
void findSol(double num1, double num2, double num3, double num4, char op1, char op2, char op3){
string solution;
// ((num1 op1 num2) op2 num3) op3 num4
if (operate(operate(operate(num1, num2, op1), num3, op2), num4, op3) == 24) {
solution = concat(concat(concat(convert(num1), convert(num2), op1), convert(num3), op2), convert(num4), op3);
addSolution(solution);
}
// (num1 op1 (num2 op2 num3)) op3 num4
if (operate(operate(num1, operate(num2, num3, op2), op1), num4, op3) == 24){
solution = concat(concat(convert(num1), concat(convert(num2), convert(num3), op2), op1), convert(num4), op3);
addSolution(solution);
}
// num1 op1 (num2 op2 (num3 op3 num4))
if (operate(num1, operate(num2, operate(num3, num4, op3), op2), op1) == 24){
solution = concat(convert(num1), concat(convert(num2), concat(convert(num3), convert(num4), op3), op2), op1);
addSolution(solution);
}
// num1 op1 ((num2 op2 num3) op3 num4)
if (operate(num1, operate(operate(num2, num3, op2), num4, op3), op1) == 24){
solution = concat(convert(num1), concat(concat(convert(num2), convert(num3), op2), convert(num4), op3), op1);
addSolution(solution);
}
// (num1 op1 num2) op2 (num3 op3 num4)
if (operate(operate(num1, num2, op1), operate(num3, num4, op3), op2) == 24){
solution = concat(concat(convert(num1), convert(num2), op1), concat(convert(num3), convert(num4), op3), op2);
addSolution(solution);
}
}
/***
* Menghapus num pada listNum, num pasti ada di listNum
*/
vector<int> removeNum(vector<int> listNum, int num){
int count = 0;
while(listNum[count] != num){
count++;
}
listNum.erase(listNum.begin()+count);
return listNum;
}
/***
* Mengembalikan jumlah semua kemungkinan yang memenuhi dari 4 angka masukan dengan
* melakukan permutasi pada 4 angka masukan dan 4 operasi yang menghasilkan (4!)*(4^3)*5
* urutan angka-operasi yang berbeda
*/
void permut(vector<int> listNum, vector<char> listOp){
for (auto num1: listNum){
vector<int> listNum2 = removeNum(listNum, num1);
for (auto num2: listNum2){
vector<int> listNum3 = removeNum(listNum2, num2);
for (auto num3: listNum3){
vector<int> listNum4 = removeNum(listNum3, num3);
for (auto num4: listNum4){
for(auto op1: listOp){
for(auto op2: listOp){
for(auto op3: listOp){
findSol(num1, num2, num3, num4, op1, op2, op3);
}
}
}
}
}
}
}
}
/***
* Mengembalikan list of string yang berisi hasil split str by space
*/
vector<string> splitString(string str){
string res = "";
vector<string> splitRes;
str = str + " ";
for(auto word: str){
if (word == ' '){
splitRes.push_back(res);
res ="";
} else{
res += word;
}
}
if (splitRes.size() != 4){
valid = false;
}
return splitRes;
}
/* *** MAIN PROGRAM *** */
int main(){
string input;
cout << R"(
__
_..-''--'----_.
,''.-''| .---/ _`-._
,' \ \ ;| | ,/ / `-._`-.
,' ,',\ \( | |// /,-._ / /
;.`. `,\ \`| |/ / | )/ /
/ /`_`.\_\ \| /_.-.'-''/ /
/ /_|_:.`. \ |;'`..') / /
`-._`-._`.`.;`.\ ,' / /
`-._`.`/ ,'-._/ /
: `-/ \`-.._/
| : ;._ (
: | \ `\.
\ \ |
: : ;
| /
; ,'
/ /
/ /
/
_ _ _ _ _ ___ ___ _____ _
| | | | ___ | | ___ ___ _____ ___ | |_ ___ |_ || | | ___ | __| ___ | | _ _ ___ ___
| | | || -_|| || _|| . || || -_| | _|| . | | _||_ ||___||__ || . || || | || -_|| _|
|_____||___||_||___||___||_|_|_||___| |_| |___| |___| |_| |_____||___||_| \_/ |___||_|
)" << endl;
cout << endl << "Enter 4 Cards [separated by space] : ";
getline (cin, input);
auto start = high_resolution_clock::now();
vector<string> listInput = splitString(input);
int a = convertInput(listInput[0]), b = convertInput(listInput[1]), c = convertInput(listInput[2]), d = convertInput(listInput[3]);
if (!valid) {
cout << "Incorrect input." << "\n\n";
} else {
vector<int> listNum{a,b,c,d};
vector<char> listOp{'+','-','x','/'};
permut(listNum, listOp);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
int numOfSolution = listSol.size();
if (numOfSolution > 0){
cout << numOfSolution << " Solutions found" << endl << endl;
for(auto sol: listSol){
cout << sol << endl;
}
string ans;
cout << "Do you want to save the solution? [yes/no] : ";
cin >> ans;
if (ans == "yes"){
string filesName;
cout << "Enter file's name (.txt) : ";
cin >> filesName;
ofstream out("..\\test\\" + filesName);
out << numOfSolution << " Solutions found" << endl;
for (auto solution: listSol){
out << solution << endl;
}
out.close();
} else if (ans != "no") {
cout << "Incorrect Input";
}
} else {
cout << "There Is No Solution." << endl;
}
cout << endl << "Time taken : " << duration.count() << " microseconds" << "\n\n";
}
}