Complete rework of the queueing system. Adding some more helper functions in maths.c. FInally, adding valgrind (a debugger) in the nix flaes file.
Stergios Upload Compile Checker / Explore-Gitea-Actions (push) Successful in 16s

This commit is contained in:
metallaxis committed 2026-07-05 02:15:26 +03:00
1 parent 953ada5b26
commit 030b73597f
9 files changed
+79 -62

No files matched your search

+1 -1
View File
@@ -1,5 +1,5 @@
name: Stergios Upload Compile Checker
run-name: ${{ gitea.actor }} has pushed to ${{ gitea.repository.name }} at ${{ gitea.ref }}
run-name: ${{ gitea.actor }} has pushed an update to Stergios Problem Solver
on: [push] # This triggers the build every time you push code
jobs:
+1 -1
View File
@@ -143,5 +143,5 @@ windows:
# Debug flag
.PHONY: debug
debug: CFLAGS += DEBUG
debug: CFLAGS += $(DEBUG)
debug: $(OUT)
+6
View File
@@ -0,0 +1,6 @@
# Things to try to implement to make the code go faster
Replace the chained hash table with open addressing. (Probably 2× speedup.)<br>
~~Replace the linked-list queue with a circular array. (10–30% speedup.)~~<br>
Implement bidirectional BFS. (Potentially 10×–100× speedup for deep searches.)<br>
Then start looking for mathematical pruning.
+1
View File
@@ -16,6 +16,7 @@
gnumake
readline
gdb
valgrind
];
shellHook = ''
+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);
/**
* @brief Returns the maximum of 3 128 bit integers
* @param a The first integer
* @param b The second integer
* @param c The third integer
* @return The maximum of the three integers
*/
int128_t max3(int128_t a, int128_t b, int128_t c);
/**
* @brief Returns the maximum of 2 128 bit integers
* @param a The first integer
* @param b The second integer
* @return The maximum of the two integers
*/
int128_t max2(int128_t a, int128_t b);
/**
* @brief Returns the minimum of 3 128 bit integers
* @param a The first integer
+10 -2
View File
@@ -40,6 +40,14 @@ int128_t gcd2(int128_t a, int128_t b) {
return a;
}
int128_t max3(int128_t a, int128_t b, int128_t c) {
return max2(max2(a, b), c);
}
int128_t max2(int128_t a, int128_t b) {
return a > b ? a : b;
}
int128_t min3(int128_t a, int128_t b, int128_t c) {
return min2(min2(a, b), c);
}
@@ -79,12 +87,12 @@ void generate_factor_pairs(int128_t* sum, int32_t prime_index, int32_t* exponent
int128_t r2a = values[1] / gcd2, r2b = values[2] / gcd2, r2c = val2 / gcd2;
if (is_new_root(r1a, r1b, r1c)) {
int128_t greater = r1b >= r1c ? r1b : r1c;
int128_t greater = max2(r1b, r1c);
LIMIT = r1a >= greater ? r1a * greater / 2 : r1b * r1c / 2;
solve(sum, r1a, r1b, r1c, print_mode);
}
if (val2 >= 0 && is_new_root(r2a, r2b, r2c)) {
int128_t greater = r2b >= r2c ? r2b : r2c;
int128_t greater = max2(r2b, r2c);
LIMIT = r2a >= greater ? r2a * greater / 2 : r2b * r2c / 2;
solve(sum, r2a, r2b, r2c, print_mode);
}
+41 -38
View File
@@ -1,63 +1,66 @@
#include "queue.h"
#include <stdio.h>
#include <stdlib.h>
static Node* queue_front = NULL;
static Node* queue_back = NULL;
static int32_t current_size = 0;
static int32_t capacity = 0;
static Node** queue;
static int32_t head;
static int32_t tail;
static int32_t size;
static int32_t capacity;
void queue_init(int32_t max_elements) {
capacity = max_elements;
queue_front = NULL;
queue_back = NULL;
current_size = 0;
queue = malloc(capacity * sizeof(*queue));
if (!queue) {
fprintf(stderr,"Queue allocation failed\n");
exit(1);
}
head = 0;
tail = 0;
size = 0;
}
void queue_push(Node* n) {
if (current_size >= capacity) return; /* Pool safety */
if (size == capacity) return;
/* Exact logic from your 'enqueue' function */
n->next = NULL;
if (!queue_back) {
queue_front = queue_back = n;
} else {
queue_back->next = n;
queue_back = n;
}
current_size++;
queue[tail] = n;
tail++;
if (tail == capacity) tail = 0;
size++;
}
Node* queue_pop() {
/* Exact logic from your 'dequeue' function */
if (!queue_front) return NULL;
Node* r = queue_front;
queue_front = queue_front->next;
if (!queue_front) {
queue_back = NULL;
}
r->next = NULL; /* Clean up the link */
current_size--;
if (size == 0) return NULL;
Node* r = queue[head];
head++;
if (head == capacity) head = 0;
size--;
return r;
}
int32_t queue_size() {
return current_size;
return size;
}
void queue_clear() {
queue_front = NULL;
queue_back = NULL;
current_size = 0;
head = 0;
tail = 0;
size = 0;
}
void queue_destroy() {
queue_front = NULL;
queue_back = NULL;
current_size = 0;
capacity = 0;
if (queue) {
free(queue);
queue = NULL;
}
head = tail = size = capacity = 0;
}
-18
View File
@@ -126,24 +126,6 @@ static bool bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_
}
bool solve(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode) {
/* Try greedy first */
int32_t g = -1;//greedy(a0, b0, c0);
if (g >= 0) {
*sum += g;
if (print_mode == 1) {
printf("\nSolution found!\n");
printf("Initial: (");
print_int128(a0); printf(", ");
print_int128(b0); printf(", ");
print_int128(c0); printf(")\n");
printf("Moves (%i): [greedy]\n", g);
} else if (print_mode == 2) {
print_int128(c0);
printf(" the required amount of moves is: %i\n", g);
}
return true;
}
/* Fallback to BFS */
return bfs(sum, a0, b0, c0, print_mode);
}
+2 -2
View File
@@ -120,7 +120,7 @@ int main() {
int128_t gcd = gcd3(a0, b0, c0);
int128_t a = a0 / gcd, b = b0 / gcd, c = c0 / gcd;
int128_t greater = b >= c ? b : c;
int128_t greater = max2(b, c);
LIMIT = a >= greater ? a * greater : b * c;
solve(&sum, a, b, c, print_mode);
@@ -145,7 +145,7 @@ int main() {
int128_t ra = a0 / gcd, rb = b0 / gcd, rc = c0 / gcd;
if (is_new_root(ra, rb, rc)) {
int128_t greater = rb >= rc ? rb : rc;
int128_t greater = max2(rb, rc);
LIMIT = ra >= greater ? ra * greater / 2 : rb * rc / 2;
solve(&sum, ra, rb, rc, print_mode);
}