From 1b62ea6e677ea61b3cec8a3721655ade81898cdb Mon Sep 17 00:00:00 2001 From: Georgios Chatzopoulos Date: Fri, 3 Jul 2026 13:52:38 +0300 Subject: [PATCH] Reorganizing the code to make more sense. --- include/node.h | 12 +++++++++--- include/solve.h | 6 ++++++ src/node.c | 46 +++++----------------------------------------- src/solve.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 44 deletions(-) diff --git a/include/node.h b/include/node.h index ded9faf..3be5d1e 100644 --- a/include/node.h +++ b/include/node.h @@ -39,10 +39,16 @@ void node_pool_destroy(); int32_t node_pool_usage(); /** - * @brief Tries to push child nodes based on the current node's move - * @param curr The current node + * @brief Returns the total capacity of the node pool + * @return The total capacity of the node pool */ -void try_push_children(Node* curr); +int32_t node_pool_capacity(); + +/** + * @brief Returns the next available node from the pool without incrementing the pointer + * @return A pointer to the next available node, or NULL if the pool is exhausted + */ +Node* node_pool_next(); /** * @brief Marks a triplet as visited and checks if it's new diff --git a/include/solve.h b/include/solve.h index c9a9cbd..26b9f1f 100644 --- a/include/solve.h +++ b/include/solve.h @@ -3,6 +3,12 @@ #include "utils.h" +/** + * @brief Tries to push child nodes based on the current node's move + * @param curr The current node + */ +void try_push_children(Node* curr); + /** * @brief Runs a solver to find the minimum moves to reach a triplet with zero * @param sum Pointer to store the sum of moves for the found solution diff --git a/src/node.c b/src/node.c index 9cc1eea..558c40e 100644 --- a/src/node.c +++ b/src/node.c @@ -58,49 +58,13 @@ int32_t node_pool_usage() { return pool_ptr; } -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)) { - if (pool_ptr >= pool_capacity) return; /* Pool safety */ - Node* n = &pool[pool_ptr++]; - n->a = na; n->b = nb; n->c = nc; - n->move = move; - n->parent = curr; - n->next = NULL; - queue_push(n); - } - } +int32_t node_pool_capacity() { + return pool_capacity; } -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); - } +Node* node_pool_next() { + if (pool_ptr >= pool_capacity) return NULL; /* Pool safety */ + return &pool[pool_ptr++]; } bool visit_and_mark(int128_t a, int128_t b, int128_t c) { diff --git a/src/solve.c b/src/solve.c index 058fee2..b0e3935 100644 --- a/src/solve.c +++ b/src/solve.c @@ -7,6 +7,51 @@ #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); + } +} + bool bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode) { node_pool_reset(); queue_clear();