Renaming bfs to solver. Adding fclose so the file doesn't remain open after use. Finally creating min2 and min3 functions.

This commit is contained in:
metallaxis committed 2026-07-03 13:43:06 +03:00
1 parent 7decb09e2f
commit 5145874485
5 files changed
+48 -14

No files matched your search

+17
View File
@@ -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); 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 * @brief Returns the absolute value of a 128 bit integer
* @param x The integer to get the absolute value of * @param x The integer to get the absolute value of
+5 -5
View File
@@ -1,10 +1,10 @@
#ifndef BFS_H #ifndef SOLVE_H
#define BFS_H #define SOLVE_H
#include "utils.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 sum Pointer to store the sum of moves for the found solution
* @param a0 The initial value of a * @param a0 The initial value of a
* @param b0 The initial value of b * @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) * @param print_mode The mode for printing the results (1 for full, 2 for minimal)
* @return 1 if a solution is found, 0 otherwise * @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 */ #endif /* SOLVE_H */
+11 -3
View File
@@ -1,6 +1,6 @@
#include "maths.h" #include "maths.h"
#include "node.h" #include "node.h"
#include "bfs.h" #include "solve.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -40,6 +40,14 @@ int128_t gcd2(int128_t a, int128_t b) {
return a; 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) { int128_t abs128(int128_t x) {
return (x < 0) ? -x : 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)) { if (is_new_root(r1a, r1b, r1c)) {
int128_t greater = r1b >= r1c ? r1b : r1c; int128_t greater = r1b >= r1c ? r1b : r1c;
LIMIT = r1a >= greater ? r1a * greater / 2 : r1b * r1c / 2; 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)) { if (val2 >= 0 && is_new_root(r2a, r2b, r2c)) {
int128_t greater = r2b >= r2c ? r2b : r2c; int128_t greater = r2b >= r2c ? r2b : r2c;
LIMIT = r2a >= greater ? r2a * greater / 2 : r2b * r2c / 2; 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; return;
+10 -2
View File
@@ -1,12 +1,13 @@
#include "bfs.h" #include "solve.h"
#include "node.h" #include "node.h"
#include "queue.h" #include "queue.h"
#include "io.h" #include "io.h"
#include "maths.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
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(); node_pool_reset();
queue_clear(); queue_clear();
clear_hash_table(); 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; moves = NULL;
} }
return true; 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);
} }
+5 -4
View File
@@ -3,7 +3,7 @@
#include "hash.h" #include "hash.h"
#include "node.h" #include "node.h"
#include "queue.h" #include "queue.h"
#include "bfs.h" #include "solve.h"
#include "io.h" #include "io.h"
#include <stdio.h> #include <stdio.h>
@@ -31,7 +31,7 @@ bool is_new_root(int128_t a, int128_t b, int128_t c) {
} }
GlobalEntry* e = malloc(sizeof(GlobalEntry)); 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->a = a; e->b = b; e->c = c;
e->next = global_root_table[h]; e->next = global_root_table[h];
global_root_table[h] = e; global_root_table[h] = e;
@@ -123,7 +123,7 @@ int main() {
int128_t greater = b >= c ? b : c; int128_t greater = b >= c ? b : c;
LIMIT = a >= greater ? a * greater : 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) { } else if (mode == 2) {
clear_global_table(); /* Reset unique root tracker */ clear_global_table(); /* Reset unique root tracker */
int32_t p = 7; int32_t p = 7;
@@ -147,12 +147,13 @@ int main() {
if (is_new_root(ra, rb, rc)) { if (is_new_root(ra, rb, rc)) {
int128_t greater = rb >= rc ? rb : rc; int128_t greater = rb >= rc ? rb : rc;
LIMIT = ra >= greater ? ra * greater / 2 : rb * rc / 2; 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: "); printf("Total sum is: ");
print_int128(sum); printf("\n"); print_int128(sum); printf("\n");
fclose(file);
} }
queue_destroy(); queue_destroy();