From 5145874485eea61083ed6e31081d7f9dba682ad8 Mon Sep 17 00:00:00 2001 From: Georgios Chatzopoulos Date: Fri, 3 Jul 2026 13:43:06 +0300 Subject: [PATCH] Renaming bfs to solver. Adding fclose so the file doesn't remain open after use. Finally creating min2 and min3 functions. --- include/maths.h | 17 +++++++++++++++++ include/{bfs.h => solve.h} | 10 +++++----- src/maths.c | 14 +++++++++++--- src/{bfs.c => solve.c} | 12 ++++++++++-- src/stergios.c | 9 +++++---- 5 files changed, 48 insertions(+), 14 deletions(-) rename include/{bfs.h => solve.h} (61%) rename src/{bfs.c => solve.c} (85%) diff --git a/include/maths.h b/include/maths.h index 1df7dc0..8107c3c 100755 --- a/include/maths.h +++ b/include/maths.h @@ -34,6 +34,23 @@ int128_t gcd3(int128_t a, int128_t b, int128_t c); */ int128_t gcd2(int128_t a, int128_t b); +/** + * @brief Returns the minimum of 3 128 bit integers + * @param a The first integer + * @param b The second integer + * @param c The third integer + * @return The minimum of the three integers + */ +int128_t min3(int128_t a, int128_t b, int128_t c); + +/** + * @brief Returns the minimum of 2 128 bit integers + * @param a The first integer + * @param b The second integer + * @return The minimum of the two integers + */ +int128_t min2(int128_t a, int128_t b); + /** * @brief Returns the absolute value of a 128 bit integer * @param x The integer to get the absolute value of diff --git a/include/bfs.h b/include/solve.h similarity index 61% rename from include/bfs.h rename to include/solve.h index 89a1edc..c9a9cbd 100644 --- a/include/bfs.h +++ b/include/solve.h @@ -1,10 +1,10 @@ -#ifndef BFS_H -#define BFS_H +#ifndef SOLVE_H +#define SOLVE_H #include "utils.h" /** - * @brief Runs a BFS to find the minimum moves to reach a triplet with zero + * @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 * @param a0 The initial value of a * @param b0 The initial value of b @@ -12,6 +12,6 @@ * @param print_mode The mode for printing the results (1 for full, 2 for minimal) * @return 1 if a solution is found, 0 otherwise */ -bool run_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); -#endif /* BFS_H */ \ No newline at end of file +#endif /* SOLVE_H */ \ No newline at end of file diff --git a/src/maths.c b/src/maths.c index 26ac392..a31f49c 100755 --- a/src/maths.c +++ b/src/maths.c @@ -1,6 +1,6 @@ #include "maths.h" #include "node.h" -#include "bfs.h" +#include "solve.h" #include #include @@ -40,6 +40,14 @@ int128_t gcd2(int128_t a, int128_t b) { return a; } +int128_t min3(int128_t a, int128_t b, int128_t c) { + return min2(min2(a, b), c); +} + +int128_t min2(int128_t a, int128_t b) { + return a > b ? b : a; +} + int128_t abs128(int128_t x) { return (x < 0) ? -x : x; } @@ -73,12 +81,12 @@ void generate_factor_pairs(int128_t* sum, int32_t prime_index, int32_t* exponent if (is_new_root(r1a, r1b, r1c)) { int128_t greater = r1b >= r1c ? r1b : r1c; LIMIT = r1a >= greater ? r1a * greater / 2 : r1b * r1c / 2; - run_bfs(sum, r1a, r1b, r1c, print_mode); + solve(sum, r1a, r1b, r1c, print_mode); } if (val2 >= 0 && is_new_root(r2a, r2b, r2c)) { int128_t greater = r2b >= r2c ? r2b : r2c; LIMIT = r2a >= greater ? r2a * greater / 2 : r2b * r2c / 2; - run_bfs(sum, r2a, r2b, r2c, print_mode); + solve(sum, r2a, r2b, r2c, print_mode); } } return; diff --git a/src/bfs.c b/src/solve.c similarity index 85% rename from src/bfs.c rename to src/solve.c index 62e7a79..058fee2 100644 --- a/src/bfs.c +++ b/src/solve.c @@ -1,12 +1,13 @@ -#include "bfs.h" +#include "solve.h" #include "node.h" #include "queue.h" #include "io.h" +#include "maths.h" #include #include -bool run_bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode) { +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(); @@ -77,4 +78,11 @@ bool run_bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mod moves = NULL; } return true; +} + +bool solve(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode) { + /* Find a better way */ + + /* Fallback to BFS */ + return bfs(sum, a0, b0, c0, print_mode); } \ No newline at end of file diff --git a/src/stergios.c b/src/stergios.c index 953d1b0..b702b32 100644 --- a/src/stergios.c +++ b/src/stergios.c @@ -3,7 +3,7 @@ #include "hash.h" #include "node.h" #include "queue.h" -#include "bfs.h" +#include "solve.h" #include "io.h" #include @@ -31,7 +31,7 @@ bool is_new_root(int128_t a, int128_t b, int128_t c) { } GlobalEntry* e = malloc(sizeof(GlobalEntry)); - if (!e) return true; /* Fallback to running BFS if malloc fails */ + if (!e) return true; /* Fallback to running the solver if malloc fails */ e->a = a; e->b = b; e->c = c; e->next = global_root_table[h]; global_root_table[h] = e; @@ -123,7 +123,7 @@ int main() { int128_t greater = b >= c ? b : c; LIMIT = a >= greater ? a * greater : b * c; - run_bfs(&sum, a, b, c, print_mode); + solve(&sum, a, b, c, print_mode); } else if (mode == 2) { clear_global_table(); /* Reset unique root tracker */ int32_t p = 7; @@ -147,12 +147,13 @@ int main() { if (is_new_root(ra, rb, rc)) { int128_t greater = rb >= rc ? rb : rc; LIMIT = ra >= greater ? ra * greater / 2 : rb * rc / 2; - run_bfs(&sum, ra, rb, rc, print_mode); + solve(&sum, ra, rb, rc, print_mode); } } printf("Total sum is: "); print_int128(sum); printf("\n"); + fclose(file); } queue_destroy();