-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_bin_old.cpp
180 lines (164 loc) · 3.81 KB
/
train_bin_old.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
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <list>
#include <set>
#include <cstring>
#include <stack>
#include <cassert>
using namespace std;
#define NUM_DIGS 4
#define PLACEHOLDER (1 << 31)
#define TARGET 10
class Binary {
public:
virtual bool works(int a, int b) = 0;
virtual int doIt(int a, int b) = 0;
};
class Plus : public Binary {
public:
bool works(int a, int b) { return true; }
int doIt(int a, int b) { return a + b; }
};
class Minus : public Binary {
public:
bool works(int a, int b) { return true; }
int doIt(int a, int b) { return a - b; }
};
class Times : public Binary {
public:
bool works(int a, int b) { return true; }
int doIt(int a, int b) { return a * b; }
};
class Divide : public Binary {
public:
bool works(int a, int b) { return b != 0 && a % b == 0; }
int doIt(int a, int b) { return a/b; }
};
class Power : public Binary {
public:
bool works(int a, int b) {
if (b < 0) {
return false;
}
if (a > 1 && b > 10) {
return false;
}
return true;
}
int doIt(int a, int b) {
int ret = 1;
for (int i = 0; i < b; i++) {
ret *= a;
}
return ret;
}
};
list<Binary*> binaries;
stack<set<int>*> dem;
void init(bool exp) {
binaries.push_back(new Plus());
binaries.push_back(new Minus());
binaries.push_back(new Times());
binaries.push_back(new Divide());
if (exp) {
binaries.push_back(new Power());
}
}
bool check(int* thingos) {
// See whether it's valid.
int ct = 0;
for (int i = 0; i < 2 * NUM_DIGS - 1; ++i) {
if (thingos[i] == PLACEHOLDER) {
--ct;
} else {
++ct;
}
if (ct == 0) {
return false;
}
}
if (ct != 1) {
return false;
}
for (int i = 0; i < 2 * NUM_DIGS - 1; ++i) {
if (thingos[i] == PLACEHOLDER) {
// Pop the last two and apply operation.
set<int>* aset = dem.top();
dem.pop();
set<int>* bset = dem.top();
dem.pop();
set<int>* newset = new set<int>();
for (set<int>::iterator a = aset->begin(); a != aset->end(); ++a) {
for (set<int>::iterator b = bset->begin(); b != bset->end(); ++b) {
for(list<Binary*>::iterator o = binaries.begin(); o != binaries.end(); ++o) {
if ((*o)->works(*a, *b)) {
newset->insert((*o)->doIt(*a, *b));
}
}
}
}
// Push the result.
dem.push(newset);
delete aset;
delete bset;
} else {
dem.push(new set<int>(thingos + i, thingos + i + 1));
}
}
assert(!dem.empty());
set<int>* res = dem.top();
dem.pop();
assert(dem.empty());
bool works = false;
for (set<int>::iterator a = res->begin(); a != res->end(); ++a) {
if (*a == TARGET) {
works = true;
break;
}
}
delete res;
return works;
}
bool can_do_it(int* digs) {
int thingos[2 * NUM_DIGS - 1];
for (int i = 0; i < NUM_DIGS; ++i) {
thingos[i] = digs[i];
}
for (int i = NUM_DIGS; i < 2 * NUM_DIGS - 1; ++i) {
thingos[i] = PLACEHOLDER;
}
sort(thingos, thingos + 2 * NUM_DIGS - 1);
do {
if (check(thingos)) {
return true;
}
} while (next_permutation(thingos, thingos + 2 * NUM_DIGS - 1));
return false;
}
int main(int argc, char ** argv) {
if (argc < 2) {
cout << "Usage: foo number [-e]" << endl;
return 0;
}
if (strlen(argv[1]) != NUM_DIGS) {
cout << "number must be " << NUM_DIGS << " digits" << endl;
return 0;
}
bool exp = false;
if (argc > 2) {
if (strcmp(argv[2], "-e") == 0) {
exp = true;
} else {
cout << "Unrecognised switch: " << argv[2] << endl;
return 0;
}
}
init(exp);
int digs[NUM_DIGS];
for (int i = 0; i < NUM_DIGS; ++i) {
digs[i] = argv[1][i] - '0';
}
cout << (can_do_it(digs) ? "1" : "0") << endl;
return 0;
}