-
Notifications
You must be signed in to change notification settings - Fork 0
/
deidos_covenant.cpp
187 lines (155 loc) · 4.97 KB
/
deidos_covenant.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
/*
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2012-2020 Oscar Riveros. all rights reserved. //
// oscar.riveros@peqnp.science //
// //
// without any restriction, Oscar Riveros reserved rights, patents and //
// commercialization of this knowledge or derived directly from this work. //
///////////////////////////////////////////////////////////////////////////////
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <fstream>
#include <iostream>
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
typedef long long U;
typedef unsigned I;
U g;
U c;
struct tape {
U *ram;
I idx;
const I len;
};
inline void write(struct tape *tape, U o) {
tape->ram[tape->idx] = o;
tape->idx++;
}
inline U read(struct tape *tape) {
tape->idx--;
return tape->ram[tape->idx];
}
inline U *list_3sort(U *lst, const I &len) {
U aux = 0;
I i = 0, j = 1;
for (i = j - 1; i < j; i++) {
for (j = i + 1; j < len - i - 1; j++) {
aux = lst[j];
if (lst[i] > aux) {
lst[j] = lst[i];
lst[i] = aux;
}
aux = lst[len - 1 - j];
if (lst[len - 1 - i] < aux) {
lst[len - 1 - j] = lst[len - 1 - i];
lst[len - 1 - i] = aux;
}
}
}
aux = lst[len / 2];
if (aux < lst[len / 2 - 1]) {
lst[len / 2] = lst[len / 2 - 1];
lst[len / 2 - 1] = aux;
}
return lst;
}
inline bool in(struct tape *tape, I o) {
for (I idx = 0; idx < tape->idx; idx++) {
if (tape->ram[idx] == o) {
return true;
}
}
return false;
}
inline void print(struct tape *uu, struct tape *vv) {
std::cout << "\nprint(sum([";
for (I idx = 0; idx < uu->len; idx++) {
if (uu->ram[idx] != 0) {
if (in(vv, idx)) {
std::cout << uu->ram[idx] << ", ";
}
}
}
std::cout << "]))" << std::endl;
std::cout << "\nprint(sum([";
for (I idx = 0; idx < uu->len; idx++) {
if (uu->ram[idx] != 0) {
if (!in(vv, idx)) {
std::cout << uu->ram[idx] << ", ";
}
}
}
std::cout << "]))\n" << std::endl;
}
inline U state(const U &s, const U &r, const U &p, const U &q) {
U l = MIN((s < q ? q - s : s - q), (r < p ? p - r : r - p));
if (l < g) {
g = l;
std::cout << "# " << c << " => " << g << std::endl;
}
return l;
}
void deidos(struct tape uu, U u, struct tape *vv, U s, U r, const U p, const U q) {
c++;
U l = state(s, r, p, q);
if (!l) {
print(&uu, vv);
g = q + p;
} else {
while (u >= l) {
U o = read(&uu);
u -= o;
s += o;
r -= o;
if (s <= p && r >= 0) {
write(vv, uu.idx);
deidos(uu, u, vv, s, r, p, q);
read(vv);
}
s -= o;
r += o;
}
}
}
int main(int argc, char *argv[]) {
printf(" \n");
printf(" ,--. o | ,---. | \n");
printf(" | |,---..,---|,---.,---. | ,---.. ,,---.,---.,---.,---.|--- \n");
printf(" | ||---'|| || |`---. | | | \\ / |---'| |,---|| || \n");
printf(" `--' `---'``---'`---'`---' `---'`---' `' `---'` '`---^` '`---' \n");
printf(" `---' by Oscar Riveros \u2122 \n");
printf(" \n");
std::ifstream file(argv[1]);
I len;
file >> len;
struct tape uu = {(U *)calloc(sizeof(U), len), 0, len};
struct tape vv = {(U *)calloc(sizeof(U), len), 0, len};
U s = 0, r = 0, t = 0, p = 0, q = 0, oo = 0, u = 0;
file >> t;
for (I i = 0; i < len; i++) {
file >> u;
write(&uu, u);
oo += u;
}
file.close();
list_3sort(uu.ram, uu.len);
p = MAX(t, oo - t);
q = MIN(t, oo - t);
s = p;
r = q;
q = oo;
p = oo;
g = oo;
c = 0;
deidos(uu, oo, &vv, s, r, p, q);
std::cout << "\nO(" << c << ")" << std::endl;
return EXIT_SUCCESS;
}