From b6063efb9007b5cbbb12ecd5a70895f134d88996 Mon Sep 17 00:00:00 2001 From: Georgios Chatzopoulos Date: Sat, 4 Jul 2026 01:08:39 +0300 Subject: [PATCH] Adding a possible useful strategy for the feature (not used in this version). But Mostly testing gitea actions. --- .gitea/workflows/demo.yaml | 13 ++ include/solve.h | 1 + src/solve.c | 141 +++++++++++++++++++- temp/stergios-int128.c | 259 ------------------------------------- 4 files changed, 153 insertions(+), 261 deletions(-) create mode 100644 .gitea/workflows/demo.yaml delete mode 100755 temp/stergios-int128.c diff --git a/.gitea/workflows/demo.yaml b/.gitea/workflows/demo.yaml new file mode 100644 index 0000000..bdab9ac --- /dev/null +++ b/.gitea/workflows/demo.yaml @@ -0,0 +1,13 @@ +name: Gitea Actions Demo +run-name: ${{ gitea.actor }} is testing Gitea Actions +on: [push] # This triggers the build every time you push code + +jobs: + Explore-Gitea-Actions: + runs-on: ubuntu-latest # Tells the Act Runner what environment to use + steps: + - name: Check out repository code + uses: actions/checkout@v4 + + - name: Run a build step + run: echo "The repository code has been checked out and is ready to build!" \ No newline at end of file diff --git a/include/solve.h b/include/solve.h index 26b9f1f..531a7ea 100644 --- a/include/solve.h +++ b/include/solve.h @@ -2,6 +2,7 @@ #define SOLVE_H #include "utils.h" +#include "node.h" /** * @brief Tries to push child nodes based on the current node's move diff --git a/src/solve.c b/src/solve.c index b0e3935..2ef195a 100644 --- a/src/solve.c +++ b/src/solve.c @@ -52,7 +52,118 @@ void try_push_children(Node* curr) { } } -bool bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode) { +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) { node_pool_reset(); queue_clear(); clear_hash_table(); @@ -126,7 +237,33 @@ bool bfs(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) { - /* Find a better way */ + /* Try patterns first */ + /*if (a0 != 0 && b0 != 0 && c0 != 0) { + int128_t pattern_steps = solve_by_pattern(a0, b0, c0); + if (pattern_steps >= 0) { + *sum += pattern_steps; + if (print_mode == 2) { + print_int128(c0); + printf(" the required amount of moves is: "); + print_int128(pattern_steps); + printf("\n"); + } else if (print_mode == 1) { + printf("\nSolution found! (pattern)\n"); + printf("Initial: ("); + print_int128(a0); printf(", "); + print_int128(b0); printf(", "); + print_int128(c0); printf(")\n"); + printf("Moves ("); + print_int128(pattern_steps); + printf("): [Computed analytically]\n"); + } + 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 */ return bfs(sum, a0, b0, c0, print_mode); diff --git a/temp/stergios-int128.c b/temp/stergios-int128.c deleted file mode 100755 index eea7b8e..0000000 --- a/temp/stergios-int128.c +++ /dev/null @@ -1,259 +0,0 @@ -#include -#include -#include - -typedef __int128 int128_t; -typedef unsigned __int128 uint128_t; - -#define HASH_SIZE 200003 -//static const int128_t LIMIT = (int128_t)1000000000ULL * (int128_t)1000000000ULL * (int128_t)100ULL; -#define LIMIT 1000000000LL - -static int is_prime(int128_t n) { - if (n < 2) return 0; - if (n % 2 == 0) return n == 2; - for (int128_t i = 3; i * i <= n; i += 2) if (n % i == 0) return 0; - return 1; -} - -typedef struct Node { - int128_t a, b, c; - char move; - struct Node *parent; - struct Node *next; -} Node; - -typedef struct VisEntry { - int128_t a, b, c; - struct VisEntry *next; -} VisEntry; - -static VisEntry *hash_table[HASH_SIZE] = {0}; - -static inline uint128_t mix128(uint128_t a, uint128_t b, uint128_t c) { - uint128_t x = a + (uint128_t)0x9e3779b97f4a7c15ULL + ((uint128_t)0x9e3779b97f4a7c15ULL << 64); - x ^= b + ((uint128_t)0xbf58476d1ce4e5b9ULL + ((uint128_t)0xbf58476d1ce4e5b9ULL << 64)) + (x << 6) + (x >> 2); - x ^= c + ((uint128_t)0x94d049bb133111ebULL + ((uint128_t)0x94d049bb133111ebULL << 64)) + (x << 6) + (x >> 2); - return x; -} - -static unsigned int hash_three128(uint128_t a, uint128_t b, uint128_t c) { - uint128_t x = mix128(a, b, c); - return (unsigned int)(x % HASH_SIZE); -} - -static unsigned int hash_three(int128_t a, int128_t b, int128_t c) { - uint128_t x = (uint128_t)(a + 0x9e3779b97f4a7c15ULL); - x ^= (uint128_t)(b + 0xbf58476d1ce4e5b9ULL) + (x<<6) + (x>>2); - x ^= (uint128_t)(c + 0x94d049bb133111ebULL) + (x<<6) + (x>>2); - return (unsigned int)(x % HASH_SIZE); -} - -static int visit_and_mark(int128_t a, int128_t b, int128_t c) { - //unsigned int h = hash_three(a,b,c); - unsigned int h = hash_three128(a,b,c); - VisEntry *cur = hash_table[h]; - while (cur) { - if (cur->a == a && cur->b == b && cur->c == c) return 0; - cur = cur->next; - } - VisEntry *e = (VisEntry*)malloc(sizeof(VisEntry)); - if (!e) { fprintf(stderr, "Out of memory\n"); exit(1); } - e->a = a; e->b = b; e->c = c; - e->next = hash_table[h]; - hash_table[h] = e; - return 1; -} - -static Node *queue_front = NULL; -static Node *queue_back = NULL; - -static void enqueue(Node *n) { - n->next = NULL; - if (!queue_back) queue_front = queue_back = n; - else { - queue_back->next = n; - queue_back = n; - } -} - -static Node *dequeue(void) { - if (!queue_front) return NULL; - Node *r = queue_front; - queue_front = queue_front->next; - if (!queue_front) queue_back = NULL; - r->next = NULL; - return r; -} - -static int safe_val(int128_t x) { - if (x > LIMIT || x < -LIMIT) return 0; - return 1; -} - -static int128_t gcd(int128_t x, int128_t y) { - while (y != 0) { - int128_t t = y; - y = x % y; - x = t; - } - return x < 0 ? -x : x; -} - - -static void try_push_children(Node *curr) { - int128_t a = curr->a, b = curr->b, c = curr->c; - - int128_t g = gcd(a, gcd(b, c)); - if (g > 1) { - int128_t ga = a / g; - int128_t gb = b / g; - int128_t gc = c / g; - - int prime_count = is_prime(ga) + is_prime(gb) + is_prime(gc); - - if (prime_count >= 1) return; - } - - int128_t na = 2*(b + c) - a; - if (safe_val(na) && safe_val(b) && safe_val(c)) { - if (visit_and_mark(na, b, c)) { - Node *n = (Node*)malloc(sizeof(Node)); - n->a = na; n->b = b; n->c = c; n->move = 'A'; n->parent = curr; n->next = NULL; - enqueue(n); - } - } - - int128_t nb = 2*(a + c) - b; - if (safe_val(a) && safe_val(nb) && safe_val(c)) { - if (visit_and_mark(a, nb, c)) { - Node *n = (Node*)malloc(sizeof(Node)); - n->a = a; n->b = nb; n->c = c; n->move = 'B'; n->parent = curr; n->next = NULL; - enqueue(n); - } - } - - int128_t nc = 2*(a + b) - c; - if (safe_val(a) && safe_val(b) && safe_val(nc)) { - if (visit_and_mark(a, b, nc)) { - Node *n = (Node*)malloc(sizeof(Node)); - n->a = a; n->b = b; n->c = nc; n->move = 'C'; n->parent = curr; n->next = NULL; - enqueue(n); - } - } -} - -// Convert string -> int128 -int128_t str_to_int128(const char *s) { - int128_t value = 0; - int sign = 1; - - if (*s == '-') { - sign = -1; - s++; - } - - while (*s >= '0' && *s <= '9') { - value = value * 10 + (*s - '0'); - s++; - } - - return sign * value; -} - -// Print __int128 (for debugging) -void print_int128(int128_t x) { - if (x == 0) { - printf("0"); - return; - } - - if (x < 0) { - putchar('-'); - x = -x; - } - - char buf[64]; - int i = 0; - - while (x > 0) { - buf[i++] = '0' + (int)(x % 10); - x /= 10; - } - - while (i--) putchar(buf[i]); -} - -int main(void) { - char sa[256], sb[256], sc[256]; - int128_t a0, b0, c0; - if (printf("Enter initial a b c: ") < 0) return 1; - if (scanf("%255s %255s %255s", sa, sb, sc) != 3) { - fprintf(stderr, "Invalid input\n"); - return 1; - } - - a0 = str_to_int128(sa); - b0 = str_to_int128(sb); - c0 = str_to_int128(sc); - - if (a0 == 0 || b0 == 0 || c0 == 0) { - printf("Already zero present: ("); - print_int128(a0); printf(", "); - print_int128(b0); printf(", "); - print_int128(c0); printf(")\n"); - return 0; - } - - if (!safe_val(a0) || !safe_val(b0) || !safe_val(c0)) { - fprintf(stderr, "Initial values exceed safe limit.\n"); - return 1; - } - - Node *root = (Node*)malloc(sizeof(Node)); - if (!root) { fprintf(stderr, "Out of memory\n"); return 1; } - root->a = a0; root->b = b0; root->c = c0; root->move = '\0'; root->parent = NULL; root->next = NULL; - enqueue(root); - visit_and_mark(a0,b0,c0); - - Node *found = NULL; - while ((root = dequeue()) != NULL) { - if (root->a == 0 || root->b == 0 || root->c == 0) { - found = root; - break; - } - try_push_children(root); - } - - if (!found) { - printf("No solution found within bounds/search limits.\n"); - return 0; - } - - int max_moves = 0; - Node *p = found; - while (p && p->move != '\0') { max_moves++; p = p->parent; } - - char *moves = (char*)malloc(max_moves + 1); - if (!moves) { fprintf(stderr, "Out of memory\n"); return 1; } - moves[max_moves] = '\0'; - p = found; - int idx = max_moves - 1; - while (p && p->move != '\0') { - moves[idx--] = p->move; - p = p->parent; - } - - printf("\nSolution found!\n"); - printf("Initial: ("); - print_int128(a0); printf(", "); - print_int128(b0); printf(", "); - print_int128(c0); printf(")\n"); - - printf("Final: ("); - print_int128(found->a); printf(", "); - print_int128(found->b); printf(", "); - print_int128(found->c); printf(")\n"); - printf("Moves (%d): %s\n", max_moves, moves); - return 0; -}