Testing gitea thing 2. (Removed the first idea now will try Greedy before BFS)
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 51s
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 51s
This commit is contained in:
1 file changed
+17
-138
+17
-138
@@ -52,117 +52,6 @@ void try_push_children(Node* curr) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void apply_move(int128_t *a, int128_t *b, int128_t *c, char move) {
|
|
||||||
if (move == 'A') *a = 2*(*b + *c) - *a;
|
|
||||||
else if (move == 'B') *b = 2*(*a + *c) - *b;
|
|
||||||
else if (move == 'C') *c = 2*(*a + *b) - *c;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int128_t solve_quadratic_k(int128_t val0, int128_t d0, int128_t d2) {
|
|
||||||
if (d2 == 0) {
|
|
||||||
/* Linear case: val0 + k * d0 = 0 */
|
|
||||||
if (d0 == 0) return -1;
|
|
||||||
if ((-val0) % d0 == 0 && (-val0) / d0 > 0) return (-val0) / d0;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
int128_t coeff = d2 / 2;
|
|
||||||
int128_t linear = d0 - coeff;
|
|
||||||
int128_t constant = val0;
|
|
||||||
|
|
||||||
int128_t disc = linear * linear - 4 * coeff * constant;
|
|
||||||
if (disc < 0) return -1;
|
|
||||||
|
|
||||||
int128_t sq = mysqrt_uint128(disc);
|
|
||||||
if (sq * sq != disc) return -1; /* Not a perfect square */
|
|
||||||
|
|
||||||
for (int sign = 1; sign >= -1; sign -= 2) {
|
|
||||||
int128_t num = -linear + sign * sq;
|
|
||||||
if (num > 0 && (2 * coeff) != 0 && num % (2 * coeff) == 0) return num / (2 * coeff);
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* After applying `prefix_len` moves from `prefix[]`, try every 2-move
|
|
||||||
* repeating pair (CA, AC, CB, BC, AB, BA). For each pair we track the
|
|
||||||
* element that the pair drives toward zero and solve the quadratic for k.
|
|
||||||
* We also try appending one extra suffix move ('A','B','C') after k pairs.
|
|
||||||
*
|
|
||||||
* Returns the total step count, or -1 if no pattern matched.
|
|
||||||
*/
|
|
||||||
static int128_t solve_by_pattern(int128_t a0, int128_t b0, int128_t c0) {
|
|
||||||
|
|
||||||
/* All short prefixes we try before the repeating part */
|
|
||||||
const char *prefixes[] = {
|
|
||||||
"", "C", "A", "B",
|
|
||||||
"CA", "CB", "AC", "BC", "AB", "BA",
|
|
||||||
"CAC", "CBC", "ACA", "ABA", "BCB", "BAB",
|
|
||||||
NULL
|
|
||||||
};
|
|
||||||
|
|
||||||
/* 6 repeating pairs: each pair is two chars, and we note which element
|
|
||||||
is NOT changed by the pair (its value is the one that gives d2 = 8*unchanged) */
|
|
||||||
const char pairs[6][3] = { "CA", "AC", "CB", "BC", "AB", "BA" };
|
|
||||||
/* unchanged element index for each pair (0=a,1=b,2=c) */
|
|
||||||
const int unchanged[6] = { 1, 1, 0, 0, 2, 2 };
|
|
||||||
|
|
||||||
/* Suffix moves after the k pairs */
|
|
||||||
const char suffixes[] = { '\0', 'A', 'B', 'C' };
|
|
||||||
|
|
||||||
for (int pi = 0; prefixes[pi] != NULL; pi++) {
|
|
||||||
/* Apply prefix */
|
|
||||||
int128_t a = a0, b = b0, c = c0;
|
|
||||||
int plen = 0;
|
|
||||||
for (const char *m = prefixes[pi]; *m; m++, plen++) apply_move(&a, &b, &c, *m);
|
|
||||||
|
|
||||||
for (int pair_i = 0; pair_i < 6; pair_i++) {
|
|
||||||
char m1 = pairs[pair_i][0];
|
|
||||||
char m2 = pairs[pair_i][1];
|
|
||||||
|
|
||||||
/* Apply one iteration of the pair to get d0 for each element */
|
|
||||||
int128_t a1 = a, b1 = b, c1 = c;
|
|
||||||
|
|
||||||
apply_move(&a1, &b1, &c1, m1);
|
|
||||||
apply_move(&a1, &b1, &c1, m2);
|
|
||||||
|
|
||||||
/* Second difference = 8 * unchanged element */
|
|
||||||
int128_t unch_val;
|
|
||||||
if (unchanged[pair_i] == 0) unch_val = a;
|
|
||||||
else if (unchanged[pair_i] == 1) unch_val = b;
|
|
||||||
else unch_val = c;
|
|
||||||
int128_t d2 = 8 * unch_val;
|
|
||||||
|
|
||||||
/* Try driving each of the 3 elements to zero */
|
|
||||||
int128_t vals0[3] = { a, b, c };
|
|
||||||
int128_t d0s[3] = { a1-a, b1-b, c1-c };
|
|
||||||
|
|
||||||
for (int ei = 0; ei < 3; ei++) {
|
|
||||||
int128_t k = solve_quadratic_k(vals0[ei], d0s[ei], d2);
|
|
||||||
if (k <= 0) continue;
|
|
||||||
|
|
||||||
/* Try each suffix (including no suffix) */
|
|
||||||
for (int si = 0; si < 4; si++) {
|
|
||||||
char suf = suffixes[si];
|
|
||||||
int slen = (suf != '\0') ? 1 : 0;
|
|
||||||
int32_t total = (int32_t)(plen + 2*k + slen);
|
|
||||||
|
|
||||||
/* Verify: Simulate k pairs then optional suffix */
|
|
||||||
int128_t va = a, vb = b, vc = c;
|
|
||||||
for (int128_t ki = 0; ki < k; ki++) {
|
|
||||||
apply_move(&va, &vb, &vc, m1);
|
|
||||||
apply_move(&va, &vb, &vc, m2);
|
|
||||||
}
|
|
||||||
if (suf != '\0') apply_move(&va, &vb, &vc, suf);
|
|
||||||
|
|
||||||
if (va == 0 || vb == 0 || vc == 0) return total;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1; /* no pattern found, fall back to BFS */
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode) {
|
static bool bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode) {
|
||||||
node_pool_reset();
|
node_pool_reset();
|
||||||
queue_clear();
|
queue_clear();
|
||||||
@@ -227,7 +116,7 @@ static bool bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_
|
|||||||
print_int128(c0);
|
print_int128(c0);
|
||||||
printf(" the required amount of moves is: %i\n", max_moves);
|
printf(" the required amount of moves is: %i\n", max_moves);
|
||||||
}
|
}
|
||||||
*sum+=max_moves;
|
*sum += max_moves;
|
||||||
|
|
||||||
if (print_mode == 1) {
|
if (print_mode == 1) {
|
||||||
free(moves);
|
free(moves);
|
||||||
@@ -237,33 +126,23 @@ static bool bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool solve(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode) {
|
bool solve(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode) {
|
||||||
/* Try patterns first */
|
/* Try greedy first */
|
||||||
/*if (a0 != 0 && b0 != 0 && c0 != 0) {
|
int32_t g = -1;//greedy(a0, b0, c0);
|
||||||
int128_t pattern_steps = solve_by_pattern(a0, b0, c0);
|
if (g >= 0) {
|
||||||
if (pattern_steps >= 0) {
|
*sum += g;
|
||||||
*sum += pattern_steps;
|
if (print_mode == 1) {
|
||||||
if (print_mode == 2) {
|
printf("\nSolution found!\n");
|
||||||
print_int128(c0);
|
printf("Initial: (");
|
||||||
printf(" the required amount of moves is: ");
|
print_int128(a0); printf(", ");
|
||||||
print_int128(pattern_steps);
|
print_int128(b0); printf(", ");
|
||||||
printf("\n");
|
print_int128(c0); printf(")\n");
|
||||||
} else if (print_mode == 1) {
|
printf("Moves (%i): [greedy]\n", g);
|
||||||
printf("\nSolution found! (pattern)\n");
|
} else if (print_mode == 2) {
|
||||||
printf("Initial: (");
|
print_int128(c0);
|
||||||
print_int128(a0); printf(", ");
|
printf(" the required amount of moves is: %i\n", g);
|
||||||
print_int128(b0); printf(", ");
|
|
||||||
print_int128(c0); printf(")\n");
|
|
||||||
printf("Moves (");
|
|
||||||
print_int128(pattern_steps);
|
|
||||||
printf("): [Computed analytically]\n");
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}*/
|
return true;
|
||||||
solve_by_pattern(a0, b0, c0); /* Call now but don't anything with it
|
}
|
||||||
Allows to compile successfully but doesn't truly
|
|
||||||
work
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* Fallback to BFS */
|
/* Fallback to BFS */
|
||||||
return bfs(sum, a0, b0, c0, print_mode);
|
return bfs(sum, a0, b0, c0, print_mode);
|
||||||
|
|||||||
Reference in new issue
Block a user