-
Notifications
You must be signed in to change notification settings - Fork 0
/
turnips.cpp
461 lines (382 loc) · 15.8 KB
/
turnips.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include <algorithm>
#include <cmath>
#include <exception>
#include <iomanip>
#include <iostream>
#include <limits>
#include <vector>
class RNG {
public:
RNG() : state{0, 0, 0, 0} {
}
RNG(uint32_t seed) {
reseed(seed);
}
void reseed(uint32_t seed) {
state[0] = 0x6C078965 * (seed ^ (seed >> 30)) + 1;
state[1] = 0x6C078965 * (state[0] ^ (state[0] >> 30)) + 2;
state[2] = 0x6C078965 * (state[1] ^ (state[1] >> 30)) + 3;
state[3] = 0x6C078965 * (state[2] ^ (state[2] >> 30)) + 4;
}
inline uint32_t getRandomUint32() {
uint32_t n = state[0] ^ (state[0] << 11);
state[0] = state[1];
state[1] = state[2];
state[2] = state[3];
state[3] = n ^ (n >> 8) ^ state[3] ^ (state[3] >> 19);
return state[3];
}
inline bool getRandomBoolean() {
return ((getRandomUint32() & 0x80000000) != 0);
}
inline int getRandomInt(int min, int max) {
return ((static_cast<uint64_t>(getRandomUint32()) *
static_cast<uint64_t>(max - min + 1)) >> 32) + min;
}
inline float getRandomFloat(float min, float max) {
uint32_t val = 0x3F800000 | (getRandomUint32() >> 9);
float fval = reinterpret_cast<float&>(val);
return min + ((fval - 1.0f) * (max - min));
}
protected:
uint32_t state[4];
};
inline int intCeil(float x) {
return static_cast<int>(x + 0.99999f);
}
class Turnips {
public:
static const size_t numberOfPatterns = 4;
static const size_t numberOfHalfDays = 14;
static const size_t numberOfPrices = 700;
inline bool computePrices(uint32_t prevPattern, RNG &rng,
uint32_t userPattern, const int32_t userPrices[numberOfHalfDays],
uint32_t &pattern, int32_t prices[numberOfHalfDays],
const bool takeSundayPriceFromInput = false, const int32_t tol = 0)
__attribute__((always_inline)) {
if (takeSundayPriceFromInput) {
prices[0] = userPrices[0];
prices[1] = userPrices[1];
} else {
prices[0] = rng.getRandomInt(90, 110);
if ((userPrices[0] > 0) && (std::abs(prices[0] - userPrices[0])) > tol) return false;
prices[1] = prices[0];
}
x = rng.getRandomInt(0, 99);
if (prevPattern == 0) {
if (x < 20) pattern = 0;
else if (x < 50) pattern = 1;
else if (x < 65) pattern = 2;
else pattern = 3;
} else if (prevPattern == 1) {
if (x < 50) pattern = 0;
else if (x < 55) pattern = 1;
else if (x < 75) pattern = 2;
else pattern = 3;
} else if (prevPattern == 2) {
if (x < 25) pattern = 0;
else if (x < 70) pattern = 1;
else if (x < 75) pattern = 2;
else pattern = 3;
} else {
if (x < 45) pattern = 0;
else if (x < 70) pattern = 1;
else if (x < 85) pattern = 2;
else pattern = 3;
}
if ((userPattern < numberOfPatterns) && (pattern != userPattern)) return false;
if (pattern == 0) {
// PATTERN 0 - "Random": high, decreasing, high, decreasing, high
decPhaseLen1 = (rng.getRandomBoolean() ? 3 : 2);
decPhaseLen2 = 5 - decPhaseLen1;
hiPhaseLen1 = rng.getRandomInt(0, 6);
hiPhaseLen2And3 = 7 - hiPhaseLen1;
hiPhaseLen3 = rng.getRandomInt(0, hiPhaseLen2And3 - 1);
t = 2;
// high phase 1
for (size_t i = 0; i < hiPhaseLen1; i++) {
prices[t++] = intCeil(rng.getRandomFloat(0.9, 1.4) * prices[0]);
if ((userPrices[t-1] > 0) && (std::abs(prices[t-1] - userPrices[t-1]) > tol)) return false;
}
// decreasing phase 1
rate = rng.getRandomFloat(0.8, 0.6);
for (size_t i = 0; i < decPhaseLen1; i++) {
prices[t++] = intCeil(rate * prices[0]);
if ((userPrices[t-1] > 0) && (std::abs(prices[t-1] - userPrices[t-1]) > tol)) return false;
rate -= 0.04;
rate -= rng.getRandomFloat(0, 0.06);
}
// high phase 2
for (size_t i = 0; i < (hiPhaseLen2And3 - hiPhaseLen3); i++) {
prices[t++] = intCeil(rng.getRandomFloat(0.9, 1.4) * prices[0]);
if ((userPrices[t-1] > 0) && (std::abs(prices[t-1] - userPrices[t-1]) > tol)) return false;
}
// decreasing phase 2
rate = rng.getRandomFloat(0.8, 0.6);
for (size_t i = 0; i < decPhaseLen2; i++) {
prices[t++] = intCeil(rate * prices[0]);
if ((userPrices[t-1] > 0) && (std::abs(prices[t-1] - userPrices[t-1]) > tol)) return false;
rate -= 0.04;
rate -= rng.getRandomFloat(0, 0.06);
}
// high phase 3
for (size_t i = 0; i < hiPhaseLen3; i++) {
prices[t++] = intCeil(rng.getRandomFloat(0.9, 1.4) * prices[0]);
if ((userPrices[t-1] > 0) && (std::abs(prices[t-1] - userPrices[t-1]) > tol)) return false;
}
} else if (pattern == 1) {
// PATTERN 1 - "Large Spike": decreasing middle, high spike, random low
peakStart = rng.getRandomInt(3, 9);
rate = rng.getRandomFloat(0.9, 0.85);
for (t = 2; t < peakStart; t++) {
prices[t] = intCeil(rate * prices[0]);
if ((userPrices[t] > 0) && (std::abs(prices[t] - userPrices[t]) > tol)) return false;
rate -= 0.03;
rate -= rng.getRandomFloat(0, 0.02);
}
prices[t++] = intCeil(rng.getRandomFloat(0.9, 1.4) * prices[0]);
if ((userPrices[t-1] > 0) && (std::abs(prices[t-1] - userPrices[t-1]) > tol)) return false;
prices[t++] = intCeil(rng.getRandomFloat(1.4, 2.0) * prices[0]);
if ((userPrices[t-1] > 0) && (std::abs(prices[t-1] - userPrices[t-1]) > tol)) return false;
prices[t++] = intCeil(rng.getRandomFloat(2.0, 6.0) * prices[0]);
if ((userPrices[t-1] > 0) && (std::abs(prices[t-1] - userPrices[t-1]) > tol)) return false;
prices[t++] = intCeil(rng.getRandomFloat(1.4, 2.0) * prices[0]);
if ((userPrices[t-1] > 0) && (std::abs(prices[t-1] - userPrices[t-1]) > tol)) return false;
prices[t++] = intCeil(rng.getRandomFloat(0.9, 1.4) * prices[0]);
if ((userPrices[t-1] > 0) && (std::abs(prices[t-1] - userPrices[t-1]) > tol)) return false;
for (; t < numberOfHalfDays; t++) {
prices[t] = intCeil(rng.getRandomFloat(0.4, 0.9) * prices[0]);
if ((userPrices[t] > 0) && (std::abs(prices[t] - userPrices[t]) > tol)) return false;
}
} else if (pattern == 2) {
// PATTERN 2 - "Decreasing": consistently decreasing
rate = 0.9;
rate -= rng.getRandomFloat(0, 0.05);
for (size_t t = 2; t < numberOfHalfDays; t++) {
prices[t] = intCeil(rate * prices[0]);
if ((userPrices[t] > 0) && (std::abs(prices[t] - userPrices[t]) > tol)) return false;
rate -= 0.03;
rate -= rng.getRandomFloat(0, 0.02);
}
} else {
// PATTERN 3 - "Small Spike": decreasing, spike, decreasing
peakStart = rng.getRandomInt(2, 9);
// decreasing phase before the peak
rate = rng.getRandomFloat(0.9, 0.4);
for (t = 2; t < peakStart; t++) {
prices[t] = intCeil(rate * prices[0]);
if ((userPrices[t] > 0) && (std::abs(prices[t] - userPrices[t]) > tol)) return false;
rate -= 0.03;
rate -= rng.getRandomFloat(0, 0.02);
}
prices[t++] = intCeil(rng.getRandomFloat(0.9, 1.4) * prices[0]);
if ((userPrices[t-1] > 0) && (std::abs(prices[t-1] - userPrices[t-1]) > tol)) return false;
prices[t++] = intCeil(rng.getRandomFloat(0.9, 1.4) * prices[0]);
if ((userPrices[t-1] > 0) && (std::abs(prices[t-1] - userPrices[t-1]) > tol)) return false;
rate = rng.getRandomFloat(1.4, 2.0);
prices[t++] = intCeil(rng.getRandomFloat(1.4, rate) * prices[0]) - 1;
if ((userPrices[t-1] > 0) && (std::abs(prices[t-1] - userPrices[t-1]) > tol)) return false;
prices[t++] = intCeil(rate * prices[0]);
if ((userPrices[t-1] > 0) && (std::abs(prices[t-1] - userPrices[t-1]) > tol)) return false;
prices[t++] = intCeil(rng.getRandomFloat(1.4, rate) * prices[0]) - 1;
if ((userPrices[t-1] > 0) && (std::abs(prices[t-1] - userPrices[t-1]) > tol)) return false;
// decreasing phase after the peak
if (t < numberOfHalfDays) {
rate = rng.getRandomFloat(0.9, 0.4);
for (; t < numberOfHalfDays; t++) {
prices[t] = intCeil(rate * prices[0]);
if ((userPrices[t] > 0) && (std::abs(prices[t] - userPrices[t]) > tol)) return false;
rate -= 0.03;
rate -= rng.getRandomFloat(0, 0.02);
}
}
}
return true;
}
protected:
int x;
size_t decPhaseLen1;
size_t decPhaseLen2;
size_t hiPhaseLen1;
size_t hiPhaseLen2And3;
size_t hiPhaseLen3;
float rate;
size_t t;
size_t peakStart;
};
void compute(int argc, char *argv[]) {
if (argc != 4) {
throw std::runtime_error("wrong number of arguments");
}
const uint32_t seed = std::stoi(argv[2]);
const uint32_t prevPattern = std::stoi(argv[3]);
uint32_t pattern;
int32_t prices[Turnips::numberOfHalfDays];
RNG rng(seed);
const uint32_t userPattern = 9999;
const int32_t userPrices[Turnips::numberOfHalfDays] = {};
Turnips turnips;
turnips.computePrices(prevPattern, rng, userPattern, userPrices, pattern, prices);
std::cout << pattern;
for (size_t t = 0; t < Turnips::numberOfHalfDays; t++) std::cout << " " << prices[t];
std::cout << std::endl;
}
void bruteForce(int argc, char *argv[]) {
if (argc != 20) {
throw std::runtime_error("wrong number of arguments");
}
const size_t numberOfWorkers = std::stoi(argv[2]);
const size_t workerId = std::stoi(argv[3]);
const uint32_t minSeed = static_cast<uint32_t>(static_cast<double>(workerId) *
static_cast<double>(0xFFFFFFFF) / static_cast<double>(numberOfWorkers));
const uint32_t maxSeed = (((numberOfWorkers > 1) && (workerId < numberOfWorkers - 1)) ?
static_cast<uint32_t>((static_cast<double>(workerId) + 1.0) *
static_cast<double>(0xFFFFFFFF) / static_cast<double>(numberOfWorkers)) :
0);
const uint64_t numberOfSeeds = static_cast<uint64_t>(maxSeed - 1) -
static_cast<uint64_t>(minSeed) + 1;
const uint32_t userPrevPattern = std::stoi(argv[4]);
const uint32_t userPattern = std::stoi(argv[5]);
int32_t userPrices[Turnips::numberOfHalfDays];
for (size_t t = 0; t < Turnips::numberOfHalfDays; t++) {
userPrices[t] = std::stoi(argv[t + 6]);
}
const uint32_t minPrevPattern = ((userPrevPattern < Turnips::numberOfPatterns) ?
userPrevPattern : 0);
const uint32_t maxPrevPattern = ((userPrevPattern < Turnips::numberOfPatterns) ?
(userPrevPattern + 1) : Turnips::numberOfPatterns);
uint64_t numberOfMatches = 0;
std::cerr << std::fixed << std::setprecision(3);
uint32_t pattern;
int32_t prices[Turnips::numberOfHalfDays];
RNG rng;
Turnips turnips;
for (uint32_t seed = minSeed; seed != maxSeed; seed++) {
if ((seed % 2000000 == 0) && (seed != minSeed)) {
const uint32_t seedsProcessed = seed - minSeed;
const double seedsProcessedPercentage = 100.0 * static_cast<double>(seedsProcessed) /
static_cast<double>(numberOfSeeds);
const uint64_t numberOfPossibleMatches =
static_cast<uint64_t>(maxPrevPattern - minPrevPattern) * seedsProcessed;
const double matchesPercentage = 100.0 * static_cast<double>(numberOfMatches) /
static_cast<double>(numberOfPossibleMatches);
std::cerr << "Worker " << workerId << ": " << seedsProcessed << "/" << numberOfSeeds <<
" processed (" << seedsProcessedPercentage << "%), " << numberOfMatches << "/" <<
numberOfPossibleMatches << " matches (" << matchesPercentage << "%)" << std::endl;
}
for (uint32_t prevPattern = minPrevPattern; prevPattern < maxPrevPattern; prevPattern++) {
rng.reseed(seed);
if (turnips.computePrices(prevPattern, rng, userPattern, userPrices, pattern, prices)) {
std::cout << seed << " " << prevPattern << " " << pattern;
for (size_t t = 0; t < Turnips::numberOfHalfDays; t++) std::cout << " " << prices[t];
std::cout << std::endl;
numberOfMatches++;
}
}
}
}
template <class T>
std::ostream& operator<<(std::ostream& stream, const std::vector<T> vector) {
for (size_t i = 0; i < vector.size(); i++) {
if (i > 0) stream << " ";
stream << vector[i];
}
return stream;
}
void sample(int argc, char *argv[]) {
if (argc != 22) {
throw std::runtime_error("wrong number of arguments");
}
const std::string sampleMode(argv[2]);
const bool verbose = (sampleMode == "verbose");
const size_t numberOfSamples = std::stoi(argv[3]);
const size_t minimumNumberOfMatches = std::stoi(argv[4]);
const uint32_t seed = std::stoi(argv[5]);
const uint32_t userPrevPattern = std::stoi(argv[6]);
const uint32_t userPattern = std::stoi(argv[7]);
int32_t userPrices[Turnips::numberOfHalfDays];
size_t now = 2;
for (size_t t = 0; t < Turnips::numberOfHalfDays; t++) {
userPrices[t] = std::stoi(argv[t + 8]);
if (userPrices[t] > 0) now = t + 1;
}
uint64_t numberOfMatches = 0;
std::cerr << std::fixed << std::setprecision(3);
size_t i = 0;
uint32_t prevPattern = userPrevPattern;
const bool takeSundayPriceFromInput = (userPrices[0] > 0);
const bool randomPrevPattern = (userPrevPattern >= Turnips::numberOfPatterns);
uint32_t pattern;
int32_t prices[Turnips::numberOfHalfDays];
const int32_t tol = 1;
RNG rng(seed);
Turnips turnips;
double x;
std::vector<size_t> prevPatternCounts(Turnips::numberOfPatterns, 0);
std::vector<size_t> patternCounts(Turnips::numberOfPatterns, 0);
std::vector<std::vector<size_t>> pricesHistogram(Turnips::numberOfHalfDays,
std::vector<size_t>(Turnips::numberOfPrices, 0));
std::vector<size_t> maxPriceHistogram(Turnips::numberOfPrices, 0);
while ((i < numberOfSamples) ||
((minimumNumberOfMatches == 0) || (numberOfMatches < minimumNumberOfMatches))) {
if ((i % 2000000 == 0) && (i > 0)) {
const double processedPercentage = 100.0 * static_cast<double>(i) /
static_cast<double>(numberOfSamples);
const double matchesPercentage = 100.0 * static_cast<double>(numberOfMatches) /
static_cast<double>(i);
std::cerr << "Seed " << seed << ": " << i << "/" << numberOfSamples << " processed (" <<
processedPercentage << "%), " << numberOfMatches << "/" << i << " matches (" <<
matchesPercentage << "%)" << std::endl;
}
if (randomPrevPattern) {
x = rng.getRandomFloat(0.0, 1.0);
if (x < 0.34627733) prevPattern = 0;
else if (x < 0.59364012) prevPattern = 1;
else if (x < 0.74124752) prevPattern = 2;
else prevPattern = 3;
}
if (turnips.computePrices(prevPattern, rng, userPattern, userPrices, pattern, prices,
takeSundayPriceFromInput, tol)) {
numberOfMatches++;
if (verbose) {
std::cout << prevPattern << " " << pattern;
for (size_t t = 0; t < Turnips::numberOfHalfDays; t++) std::cout << " " << prices[t];
std::cout << std::endl;
} else {
prevPatternCounts[prevPattern]++;
patternCounts[pattern]++;
for (size_t t = 0; t < Turnips::numberOfHalfDays; t++) {
pricesHistogram[t][prices[t]]++;
}
if (now < Turnips::numberOfHalfDays) {
maxPriceHistogram[*std::max_element(std::begin(prices) + now, std::end(prices))]++;
}
}
}
i++;
}
if (!verbose) {
std::cout << numberOfMatches << " " << prevPatternCounts << " " << patternCounts << " " <<
pricesHistogram << " " << maxPriceHistogram << std::endl;
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
throw std::runtime_error("missing mode");
}
const std::string mode(argv[1]);
if (mode == "compute") {
// turnips compute <seed> <prevPattern>
compute(argc, argv);
} else if (mode == "bruteForce") {
// turnips bruteForce <numberOfWorkers> <workerId> <prevPattern> <pattern> <prices[0:14]>
bruteForce(argc, argv);
} else if (mode == "sample") {
// turnips sample <sampleMode> <numberOfSamples> <minimumNumberOfMatches> <seed> <prevPattern>
// <pattern> <prices[0:14]>
sample(argc, argv);
} else {
throw std::runtime_error("invalid mode");
}
return 0;
}