Skip to content

Commit 5bc5c8a

Browse files
Add files via upload
1 parent 6ab381a commit 5bc5c8a

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

MasterMindTurbo.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ template <uFast A> struct get_power<A, 0> { static const uFast value = 1; };
2323
const uFast s = get_power<c, n>::value; // Number of combinations (c^n)
2424
const uFast imark0_val = (uFast)(double(n) * ((double(n) / 2.0) + 1.5));
2525

26+
// Replace slow decimal math with fast, packed base-c integers.
2627
struct code_t {
2728
uFast v;
2829
// Decode into n digits (base c)
@@ -52,11 +53,13 @@ struct code_t {
5253

5354
static code_t Valids[s + 1];
5455

56+
// New FillSet for base-c code_t
5557
void FillSet(uFast current_code, uFast pos, uFast& counter) {
5658
if (pos == n) { Valids[counter++] = { current_code }; return; }
5759
for (uFast i = 0; i < c; ++i) FillSet(current_code * c + i, pos + 1, counter);
5860
}
5961

62+
// --- OPTIMIZATION 2: REWRITTEN SCORING KERNEL ---
6063
static uFast iMark[imark0_val + 1];
6164
static uFast mark_to_idx[n + 1][n + 1];
6265

@@ -81,8 +84,10 @@ inline uFast score(code_t guess, code_t secret) {
8184
std::array<uFast, n> g_digits, s_digits;
8285
guess.unpack(g_digits);
8386
secret.unpack(s_digits);
87+
8488
uFast black = 0;
8589
std::array<uFast, c> g_counts{}, s_counts{};
90+
8691
for (uFast i = 0; i < n; ++i) {
8792
if (g_digits[i] == s_digits[i]) {
8893
black++;
@@ -241,7 +246,8 @@ int main() {
241246
for (guess = g[lvl]; guess <= ubPure[lvl]; guess++) {
242247
for (sign = r[lvl]; sign <= imark0; sign++) {
243248
if (MapConsistent(lvl, guess, sign) == 0) continue;
244-
g[lvl] = guess; r[lvl] = sign;
249+
g[lvl] = guess;
250+
r[lvl] = sign;
245251
if (sign == imark0) {
246252
x++;
247253
RowSum[x] = lvl;
@@ -278,18 +284,21 @@ int main() {
278284
p = MapConsistent(l, g[l], r[l]);
279285
x0 = x - (ubPure[lvl] * p);
280286
for (i = 1; i <= ubPure[lvl]; i++) {
281-
GuessSum[i] = 0;
282-
for (j = 1; j <= p; j++) GuessSum[i] += RowSum[x0 + j + ((i - 1) * p)];
283-
if (GuessSum[i] < gMin) {
284-
gMin = GuessSum[i];
287+
int sum = 0;
288+
int baseOffset = x0 + (i - 1) * p;
289+
for (j = 1; j <= p; j++) sum += RowSum[baseOffset + j];
290+
GuessSum[i] = sum;
291+
if (sum < gMin) {
292+
gMin = sum;
285293
k = i;
286294
}
287295
}
296+
int sourceBase = x0 + (k - 1) * p;
288297
for (i = 1; i <= p; i++) {
289-
l = x0 + i;
290-
m = l + ((k - 1) * p);
291-
RowSum[l] = RowSum[m];
292-
for (j = 1; j < maxdepth; j++) list[j][l] = list[j][m];
298+
int destIndex = x0 + i;
299+
int sourceIndex = sourceBase + i;
300+
RowSum[destIndex] = RowSum[sourceIndex];
301+
for (j = 1; j < maxdepth; j++) list[j][destIndex] = list[j][sourceIndex];
293302
}
294303
x = x0 + p;
295304
}
@@ -311,4 +320,4 @@ int main() {
311320
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
312321
std::cin.get();
313322
return 0;
314-
}
323+
}

0 commit comments

Comments
 (0)