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
Stergios Upload Compile Checker / Explore-Gitea-Actions (push) Successful in 16s
This commit is contained in:
9 files changed
+76
-59
No files matched your search
@@ -1,5 +1,5 @@
|
|||||||
name: Stergios Upload Compile Checker
|
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
|
on: [push] # This triggers the build every time you push code
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
|||||||
@@ -143,5 +143,5 @@ windows:
|
|||||||
|
|
||||||
# Debug flag
|
# Debug flag
|
||||||
.PHONY: debug
|
.PHONY: debug
|
||||||
debug: CFLAGS += DEBUG
|
debug: CFLAGS += $(DEBUG)
|
||||||
debug: $(OUT)
|
debug: $(OUT)
|
||||||
@@ -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.
|
||||||
@@ -16,6 +16,7 @@
|
|||||||
gnumake
|
gnumake
|
||||||
readline
|
readline
|
||||||
gdb
|
gdb
|
||||||
|
valgrind
|
||||||
];
|
];
|
||||||
|
|
||||||
shellHook = ''
|
shellHook = ''
|
||||||
|
|||||||
@@ -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 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
|
* @brief Returns the minimum of 3 128 bit integers
|
||||||
* @param a The first integer
|
* @param a The first integer
|
||||||
|
|||||||
+10
-2
@@ -40,6 +40,14 @@ int128_t gcd2(int128_t a, int128_t b) {
|
|||||||
return a;
|
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) {
|
int128_t min3(int128_t a, int128_t b, int128_t c) {
|
||||||
return min2(min2(a, b), 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;
|
int128_t r2a = values[1] / gcd2, r2b = values[2] / gcd2, r2c = val2 / gcd2;
|
||||||
|
|
||||||
if (is_new_root(r1a, r1b, r1c)) {
|
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;
|
LIMIT = r1a >= greater ? r1a * greater / 2 : r1b * r1c / 2;
|
||||||
solve(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 = max2(r2b, r2c);
|
||||||
LIMIT = r2a >= greater ? r2a * greater / 2 : r2b * r2c / 2;
|
LIMIT = r2a >= greater ? r2a * greater / 2 : r2b * r2c / 2;
|
||||||
solve(sum, r2a, r2b, r2c, print_mode);
|
solve(sum, r2a, r2b, r2c, print_mode);
|
||||||
}
|
}
|
||||||
|
|||||||
+38
-35
@@ -1,63 +1,66 @@
|
|||||||
#include "queue.h"
|
#include "queue.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
static Node* queue_front = NULL;
|
static Node** queue;
|
||||||
static Node* queue_back = NULL;
|
static int32_t head;
|
||||||
static int32_t current_size = 0;
|
static int32_t tail;
|
||||||
static int32_t capacity = 0;
|
static int32_t size;
|
||||||
|
static int32_t capacity;
|
||||||
|
|
||||||
void queue_init(int32_t max_elements) {
|
void queue_init(int32_t max_elements) {
|
||||||
capacity = max_elements;
|
capacity = max_elements;
|
||||||
queue_front = NULL;
|
queue = malloc(capacity * sizeof(*queue));
|
||||||
queue_back = NULL;
|
|
||||||
current_size = 0;
|
if (!queue) {
|
||||||
|
fprintf(stderr,"Queue allocation failed\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
head = 0;
|
||||||
|
tail = 0;
|
||||||
|
size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void queue_push(Node* n) {
|
void queue_push(Node* n) {
|
||||||
if (current_size >= capacity) return; /* Pool safety */
|
if (size == capacity) return;
|
||||||
|
|
||||||
/* Exact logic from your 'enqueue' function */
|
queue[tail] = n;
|
||||||
n->next = NULL;
|
tail++;
|
||||||
if (!queue_back) {
|
|
||||||
queue_front = queue_back = n;
|
if (tail == capacity) tail = 0;
|
||||||
} else {
|
|
||||||
queue_back->next = n;
|
size++;
|
||||||
queue_back = n;
|
|
||||||
}
|
|
||||||
current_size++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Node* queue_pop() {
|
Node* queue_pop() {
|
||||||
/* Exact logic from your 'dequeue' function */
|
if (size == 0) return NULL;
|
||||||
if (!queue_front) return NULL;
|
|
||||||
|
|
||||||
Node* r = queue_front;
|
Node* r = queue[head];
|
||||||
queue_front = queue_front->next;
|
head++;
|
||||||
|
|
||||||
if (!queue_front) {
|
if (head == capacity) head = 0;
|
||||||
queue_back = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
r->next = NULL; /* Clean up the link */
|
|
||||||
current_size--;
|
|
||||||
|
|
||||||
|
size--;
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t queue_size() {
|
int32_t queue_size() {
|
||||||
return current_size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
void queue_clear() {
|
void queue_clear() {
|
||||||
queue_front = NULL;
|
head = 0;
|
||||||
queue_back = NULL;
|
tail = 0;
|
||||||
current_size = 0;
|
size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void queue_destroy() {
|
void queue_destroy() {
|
||||||
queue_front = NULL;
|
if (queue) {
|
||||||
queue_back = NULL;
|
free(queue);
|
||||||
current_size = 0;
|
queue = NULL;
|
||||||
capacity = 0;
|
}
|
||||||
|
|
||||||
|
head = tail = size = capacity = 0;
|
||||||
}
|
}
|
||||||
-18
@@ -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) {
|
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 */
|
/* Fallback to BFS */
|
||||||
return bfs(sum, a0, b0, c0, print_mode);
|
return bfs(sum, a0, b0, c0, print_mode);
|
||||||
}
|
}
|
||||||
+2
-2
@@ -120,7 +120,7 @@ int main() {
|
|||||||
int128_t gcd = gcd3(a0, b0, c0);
|
int128_t gcd = gcd3(a0, b0, c0);
|
||||||
int128_t a = a0 / gcd, b = b0 / gcd, c = c0 / gcd;
|
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;
|
LIMIT = a >= greater ? a * greater : b * c;
|
||||||
|
|
||||||
solve(&sum, a, b, c, print_mode);
|
solve(&sum, a, b, c, print_mode);
|
||||||
@@ -145,7 +145,7 @@ int main() {
|
|||||||
int128_t ra = a0 / gcd, rb = b0 / gcd, rc = c0 / gcd;
|
int128_t ra = a0 / gcd, rb = b0 / gcd, rc = c0 / gcd;
|
||||||
|
|
||||||
if (is_new_root(ra, rb, rc)) {
|
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;
|
LIMIT = ra >= greater ? ra * greater / 2 : rb * rc / 2;
|
||||||
solve(&sum, ra, rb, rc, print_mode);
|
solve(&sum, ra, rb, rc, print_mode);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user