gimkit

Searching for the optimal Gimkit strategy

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  131. 131
  132. 132
  133. 133
  134. 134
  135. 135
  136. 136
  137. 137
  138. 138
  139. 139
  140. 140
  141. 141
  142. 142
  143. 143
  144. 144
  145. 145
  146. 146
  147. 147
  148. 148
  149. 149
  150. 150
  151. 151
  152. 152
  153. 153
  154. 154
  155. 155
  156. 156
  157. 157
  158. 158
  159. 159
  160. 160
  161. 161
  162. 162
  163. 163
  164. 164
  165. 165
  166. 166
  167. 167
  168. 168
  169. 169
  170. 170
  171. 171
  172. 172
  173. 173
  174. 174
  175. 175
  176. 176
  177. 177
  178. 178
  179. 179
  180. 180
  181. 181
  182. 182
  183. 183
  184. 184
  185. 185
  186. 186
  187. 187
  188. 188
  189. 189
  190. 190
  191. 191
  192. 192
  193. 193
  194. 194
  195. 195
  196. 196
  197. 197
  198. 198
  199. 199
  200. 200
  201. 201
  202. 202
  203. 203
  204. 204
  205. 205
  206. 206
  207. 207
  208. 208
  209. 209
  210. 210
  211. 211
  212. 212
  213. 213
  214. 214
  215. 215
  216. 216
  217. 217
  218. 218
  219. 219
  220. 220
  221. 221
  222. 222
  223. 223
  224. 224
  225. 225
  226. 226
  227. 227
  228. 228
  229. 229
  230. 230
  231. 231
  232. 232
  233. 233
  234. 234
  235. 235
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;

// DP Gimkit Solver
// Developed by Ta180m

// Initial conditions
// Change these values to alter the initial state
ll start = 0, goal = 1e10; // Start and end amounts
int max_it = 150; // Number of iterations to solve
int MPQ = 0, SB = 0, M = 0; // Initial upgrade status
int D = 0, R = 0, B1 = 0, B2 = 0; // Initial power-up status

// State: Iteration, Upgrade status, Power-up status
// DP array stores maximum possible money for each state
// pre array used to reconstruct optimal strategy
ll DP[7200000], pre[7200000];

// Upgrade values
double val[3][10] = {
    { 1, 5, 50, 100, 500, 2000, 5000, 10000, 250000, 1000000 },
    { 2, 20, 100, 200, 1000, 4000, 10000, 50000, 1000000, 5000000 },
    { 1, 1.5, 2, 3, 5, 8, 12, 18, 30, 100 }
};

// Upgrade costs
ll cost[2][3][10] = {
    { { 0, 10, 100, 1000, 10000, 75000, 300000, 1000000, 10000000, 100000000},
    { 0, 15, 150, 1500, 15000, 115000, 450000, 1500000, 15000000, 200000000 },
    { 0, 50, 300, 2000, 12000, 85000, 700000, 6500000, 65000000, 1000000000 } },
    { { 0, 8, 75, 750, 7500, 56250, 225000, 750000, 7500000, 75000000 },
    { 0, 12, 113, 1125, 11250, 86250, 337500, 1125000, 11250000, 150000000 },
    { 0, 38, 225, 1500, 9000, 63750, 525000, 4875000, 48750000, 750000000 } }
};

// Power-up costs
int base[4] = { 250, 1000, 20, 50 };
double pcent[4] = { 0.16, 0.30, 0.03, 0.06 };
ll pcost(int i, ll x) { return 5 * ceil((double)(pcent[i] * x + base[i]) / 5); }

// Utility function to format output
string format(ll x) {
    string ret = to_string(x);
    int pos = ret.length() - 3;
    while (pos > 0) {
        ret.insert(pos, ",");
        pos -= 3;
    }
    return ret;
}

// Bottom-up DP algorithm
int main() {
    // Initialize DP array
    memset(DP, -1, sizeof(DP));
    
    // Calculate initial state
    int s = 3600 * MPQ + 360 * SB + 36 * M + 18 * D + 9 * R + 3 * B1 + B2;
    
    // Set initial value
    DP[s] = start;
    for (int i = 0; i < 36000 * max_it; i++) {
        if (DP[i] != -1) {
            // Calculate parameters
            int it = i / 36000;
            int MPQ = (i / 3600) % 10, SB = (i / 360) % 10, M = (i / 36) % 10;
            int D = (i / 18) % 2, R = (i / 9) % 2, B1 = (i / 3) % 3, B2 = i % 3;
            
            // Income per question
            ll inc = round((val[0][MPQ] + (i != 0 ? val[1][SB] : 0)) * val[2][M]);
            
            // Answer a question
            if (DP[i] + inc > DP[i + 36000]) {
                DP[i + 36000] = DP[i] + inc;
                pre[i + 36000] = i;
            }
            
            // Answer a question using the mini bonus
            if (B1 == 1 && DP[i] + 2 * inc > DP[i + 36003]) {
                DP[i + 36003] = DP[i] + 2 * inc;
                pre[i + 36003] = i;
            }
            
            // Answer a question using the mega bonus
            if (B2 == 1 && DP[i] + 5 * inc > DP[i + 36001]) {
                DP[i + 36001] = DP[i] + 5 * inc;
                pre[i + 36001] = i;
            }
            
            // Answer a question using both the mini bonus and the mega bonus
            if (B1 == 1 && B2 == 1 && DP[i] + 10 * inc > DP[i + 36004]) {
                DP[i + 36004] = DP[i] + 10 * inc;
                pre[i + 36004] = i;
            }
            
            // Upgrade money per question
            for (int j = MPQ + 1; j < 10 && DP[i] >= cost[D][0][j]; j++) {
                if (DP[i] - cost[D][0][j] > DP[i + 3600 * (j - MPQ)]) {
                    DP[i + 3600 * (j - MPQ)] = DP[i] - cost[D][0][j];
                    pre[i + 3600 * (j - MPQ)] = i;
                }
            }
            
            // Upgrade streak bonus
            for (int j = SB + 1; j < 10 && DP[i] >= cost[D][1][j]; j++) {
                if (DP[i] - cost[D][1][j] > DP[i + 360 * (j - SB)]) {
                    DP[i + 360 * (j - SB)] = DP[i] - cost[D][1][j];
                    pre[i + 360 * (j - SB)] = i;
                }
            }
            
            // Upgrade multiplier
            for (int j = M + 1; j < 10 && DP[i] >= cost[D][2][j]; j++) {
                if (DP[i] - cost[D][2][j] > DP[i + 36 * (j - M)]) {
                    DP[i + 36 * (j - M)] = DP[i] - cost[D][2][j];
                    pre[i + 36 * (j - M)] = i;
                }
            }
            
            // Buy the discounter
            if (D == 0 && DP[i] - pcost(0, DP[i]) > DP[i + 18]) {
                DP[i + 18] = DP[i] - pcost(0, DP[i]);
                pre[i + 18] = i;
            }
            
            // Buy the rebooter
            if (R == 0 && DP[i] - pcost(1, DP[i]) > DP[i - i % 9 + 9]) {
                DP[i - i % 9 + 9] = DP[i] - pcost(1, DP[i]);
                pre[i - i % 9 + 9] = i;
            }
            
            // Buy the mini bonus
            if (B1 == 0 && DP[i] - pcost(2, DP[i]) > DP[i + 2]) {
                DP[i + 3] = DP[i] - pcost(2, DP[i]);
                pre[i + 3] = i;
            }
            
            // Buy the mega bonus
            if (B2 == 0 && DP[i] - pcost(3, DP[i]) > DP[i + 1]) {
                DP[i + 1] = DP[i] - pcost(3, DP[i]);
                pre[i + 1] = i;
            }
        }
    }
    
    // Find the optimal solution
    int sol = 7200000;
    for (int i = 0; i < 36000 * max_it; i++) {
        if (i / 36000 > sol / 36000) break;
        if (DP[i] >= goal && (sol == 7200000 || DP[i] > DP[sol])) sol = i;
    }
    
    // Print output
    if (sol != 7200000) {
        vector<int> output;
        for (int i = sol; i != s; i = pre[i]) output.push_back(i);
        reverse(output.begin(), output.end());
        cout << "Iterations: " << sol / 36000 << endl;
        for (int i = 0; i < output.size(); i++) {
            int it = output[i] / 36000;
            int MPQ = (output[i] / 3600) % 10, SB = (output[i] / 360) % 10, M = (output[i] / 36) % 10;
            int D = (output[i] / 18) % 2, R = (output[i] / 9) % 2, B1 = (output[i] / 3) % 3, B2 = output[i] % 3;
            
            // Output iteration
            if (it != output[i - 1] / 36000) cout << right << setw(3) << it << ". ";
            else cout << "     ";
            
            // Answer a question
            if (it != output[i - 1] / 36000 && B1 == (output[i - 1] / 3) % 3 && B2 == output[i - 1] % 3) {
                cout << "Answer 1 question, bringing your total up to $" << format(DP[output[i]]) << endl;
            }
            
            // Answer a question with the mini bonus
            else if (it != output[i - 1] / 36000 && B1 != (output[i - 1] / 3) % 3 && B2 == output[i - 1] % 3) {
                cout << "Answer 1 question using the mini bonus, bringing your total up to $" << format(DP[output[i]]) << endl;
            }
            
            // Answer a question using the mega bonus
            else if (it != output[i - 1] / 36000 && B1 == (output[i - 1] / 3) % 3 && B2 != output[i - 1] % 3) {
                cout << "Answer 1 question using the mega bonus, bringing your total up to $" << format(DP[output[i]]) << endl;
            }
            
            // Answer a question using both the mini and mega bonuses
            else if (it != output[i - 1] / 36000 && B1 != (output[i - 1] / 3) % 3 && B2 != output[i - 1] % 3) {
                cout << "Answer 1 question using both the mini and mega bonuses, bringing your total up to $" << format(DP[output[i]]) << endl;
            }
            
            // Upgrade money per question
            else if (MPQ != (output[i - 1] / 3600) % 10) {
                cout << "Buy the Level " << MPQ << " ($" << format(val[0][MPQ]) << ") money per question upgrade for $" << format(cost[D][0][MPQ]) << ", making your total $" << format(DP[output[i]]) << endl;
            }
            
            // Upgrade streak bonus
            else if (SB != (output[i - 1] / 360) % 10) {
                cout << "Buy the Level " << SB << " ($" << format(val[1][SB]) << ") streak bonus upgrade for $" << format(cost[D][1][SB]) << ", making your total $" << format(DP[output[i]]) << endl;
            }
            
            // Upgrade multiplier
            else if (M != (output[i - 1] / 36) % 10) {
                cout << "Buy the Level " << M << " (x" << format(val[2][M]) << ") multiplier for $" << format(cost[D][2][M]) << ", making your total $" << format(DP[output[i]]) << endl;
            }
            
            // Buy the discounter
            else if (D != (output[i - 1] / 18) % 2) {
                cout << "Buy and use the discounter for $" << format(pcost(0, DP[output[i - 1]])) << ", making your total $" << format(DP[output[i]]) << endl;
            }
            
            // Buy the rebooter
            else if (R != (output[i - 1] / 9) % 2) {
                cout << "Buy and use the rebooter for $" << format(pcost(1, DP[output[i - 1]])) << ", making your total $" << format(DP[output[i]]) << endl;
            }
            
            // Buy the mini bonus
            else if (B1 != (output[i - 1] / 3) % 3) {
                cout << "Buy the mini bonus for $" << format(pcost(2, DP[output[i - 1]])) << ", making your total $" << format(DP[output[i]]) << endl;
            }
            
            // Buy the mega bonus
            else if (B2 != output[i - 1] % 3) {
                cout << "Buy the mega bonus for $" << format(pcost(3, DP[output[i - 1]])) << ", making your total $" << format(DP[output[i]]) << endl;
            }
            
            // Error
            else cout << "Error: Something went wrong" << endl;
        }
    }
    else cout << "No strategy to reach $" << goal << " from $" << start << " in " << max_it << " iterations could be found" << endl;
}