#include "solve.h" #include "node.h" #include "queue.h" #include "io.h" #include "maths.h" #include #include static void make_move(char move, Node* curr) { int128_t na = curr->a, nb = curr->b, nc = curr->c; /* Calculate the new triplet based on the move type */ if (move == 'A') { na = 2 * (curr->b + curr->c) - curr->a; } else if (move == 'B') { nb = 2 * (curr->a + curr->c) - curr->b; } else if (move == 'C') { nc = 2 * (curr->a + curr->b) - curr->c; } /* Validation and Enqueue */ if (safe_val(na) && safe_val(nb) && safe_val(nc)) { if (visit_and_mark(na, nb, nc)) { Node* n = node_pool_next(); if (!n) return; /* Node pool exhausted, skip this move */ n->a = na; n->b = nb; n->c = nc; n->move = move; n->parent = curr; n->next = NULL; queue_push(n); } } } void try_push_children(Node* curr) { char last = curr->move; if (last == '\0') { make_move('A', curr); make_move('B', curr); make_move('C', curr); } else if (last == 'A') { make_move('C', curr); make_move('B', curr); } else if (last == 'B') { make_move('C', curr); make_move('A', curr); } else if (last == 'C') { make_move('B', curr); make_move('A', curr); } } static bool bfs(int128_t* sum, int128_t gcd, int128_t a0, int128_t b0, int128_t c0, int print_mode, FILE* output) { node_pool_reset(); queue_clear(); clear_hash_table(); Node* root = node_pool_next(); if (!root) return false; root->a = a0; root->b = b0; root->c = c0; root->move = '\0'; root->parent = NULL; queue_push(root); visit_and_mark(a0, b0, c0); Node* found = NULL; while (queue_size() > 0) { Node* curr = queue_pop(); /* Goal Check */ if (curr->a == 0 || curr->b == 0 || curr->c == 0) { found = curr; break; } try_push_children(curr); } if (!found) { fprintf(output, "No solution found for ("); fprint_int128(output, a0 * gcd); fprintf(output, ", "); fprint_int128(output, b0 * gcd); fprintf(output, ", "); fprint_int128(output, c0 * gcd); fprintf(output, ").\n"); return false; } int32_t max_moves = 0; for (Node* p = found; p && p->move != '\0'; p = p->parent) max_moves++; char* moves; if (print_mode == 1) { moves = malloc(max_moves + 1); if (!moves) { fprintf(stderr, "Out of memory\n"); return false; } moves[max_moves] = '\0'; Node* p = found; int32_t idx = max_moves - 1; while (p && p->move != '\0') { moves[idx--] = p->move; p = p->parent; } if (output) { int128_t a = a0 * gcd, b = b0 * gcd, c = c0 * gcd; fprintf(output, "\nSolution found!\n"); fprintf(output, "Initial: ("); fprint_int128(output, a); fprintf(output, ", "); fprint_int128(output, b); fprintf(output, ", "); fprint_int128(output, c); fprintf(output, ")\n"); fprintf(output, "Final: ("); fprint_int128(output, found->a); fprintf(output, ", "); fprint_int128(output, found->b); fprintf(output, ", "); fprint_int128(output, found->c); fprintf(output, ")\n"); fprintf(output, "Moves (%i): %s\n", max_moves, moves); } } else if (print_mode == 2) { if (output) { int128_t a = a0 * gcd, b = b0 * gcd, c = c0 * gcd; fprintf(output, "For ("); fprint_int128(output, a); fprintf(output, ", "); fprint_int128(output, b); fprintf(output, ", "); fprint_int128(output, c); fprintf(output, ") the required amount of moves is: %i\n", max_moves); } } else if (print_mode == 3) { if (output) { int128_t a = a0 * gcd, b = b0 * gcd, c = c0 * gcd; int128_t k = mysqrt_uint128(a * a + b * b + c * c - 2 *a * b - 2 * b * c - 2 * c * a); fprint_int128(output, a); fprintf(output, " "); fprint_int128(output, b); fprintf(output, " "); fprint_int128(output, c); fprintf(output, " "); fprint_int128(output, max_moves); fprintf(output, " "); fprint_int128(output, gcd2(a, c)); fprintf(output, " "); fprint_int128(output, gcd2(b, c)); fprintf(output, " "); fprint_int128(output, c % b); fprintf(output, " "); fprint_int128(output, c % a); fprintf(output, " "); fprint_int128(output, k); fprintf(output, " "); fprint_int128(output, c - a - b - k); fprintf(output, " "); fprint_int128(output, c - a - b + k); fprintf(output, " "); fprintf(output, "%s", k < 2 * (int128_t)mysqrt_uint128(a * b) ? "True" : "False"); fprintf(output, "\n"); } } *sum += max_moves; if (print_mode == 1) { free(moves); moves = NULL; } return true; } bool solve(int128_t* sum, int128_t gcd, int128_t a0, int128_t b0, int128_t c0, int print_mode, FILE* output) { /* Execute to BFS */ return bfs(sum, gcd, a0, b0, c0, print_mode, output); }