Uploading my current work to my personal gitea
This commit is contained in:
9 files changed
+1026
No files matched your search
Executable
+283
@@ -0,0 +1,283 @@
|
||||
#include "core.h"
|
||||
#include "maths.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define HASH_SIZE 200003
|
||||
//#define LIMIT 100000000LL
|
||||
|
||||
//#define HASH_SIZE 200000003
|
||||
//const int128_t LIMIT = (int128_t)1000000000ULL * 1000000000ULL * 1000000000ULL;
|
||||
|
||||
int128_t LIMIT = 0;
|
||||
|
||||
typedef struct Node {
|
||||
int128_t a, b, c;
|
||||
char move;
|
||||
struct Node* parent;
|
||||
struct Node* next;
|
||||
} Node;
|
||||
|
||||
typedef struct VisEntry {
|
||||
int128_t a, b, c;
|
||||
struct VisEntry* next;
|
||||
} VisEntry;
|
||||
|
||||
static VisEntry* hash_table[HASH_SIZE] = {0};
|
||||
|
||||
void clear_hash_table() {
|
||||
for (size_t i = 0; i < HASH_SIZE; ++i) {
|
||||
VisEntry* cur = hash_table[i];
|
||||
while (cur) {
|
||||
VisEntry* nx = cur->next;
|
||||
free(cur);
|
||||
cur = nx;
|
||||
}
|
||||
|
||||
hash_table[i] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
int run_bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode);
|
||||
|
||||
static inline uint128_t mix128(uint128_t a, uint128_t b, uint128_t c) {
|
||||
uint128_t x = a + (uint128_t)0x9e3779b97f4a7c15ULL + ((uint128_t)0x9e3779b97f4a7c15ULL << 64);
|
||||
x ^= b + ((uint128_t)0xbf58476d1ce4e5b9ULL + ((uint128_t)0xbf58476d1ce4e5b9ULL << 64)) + (x << 6) + (x >> 2);
|
||||
x ^= c + ((uint128_t)0x94d049bb133111ebULL + ((uint128_t)0x94d049bb133111ebULL << 64)) + (x << 6) + (x >> 2);
|
||||
return x;
|
||||
}
|
||||
|
||||
static unsigned int hash_three128(uint128_t a, uint128_t b, uint128_t c) {
|
||||
uint128_t x = mix128(a, b, c);
|
||||
return (unsigned int)(x % HASH_SIZE);
|
||||
}
|
||||
|
||||
static int visit_and_mark(int128_t a, int128_t b, int128_t c) {
|
||||
unsigned int h = hash_three128(a,b,c);
|
||||
VisEntry* cur = hash_table[h];
|
||||
while (cur) {
|
||||
if (cur->a == a && cur->b == b && cur->c == c) return 0;
|
||||
cur = cur->next;
|
||||
}
|
||||
VisEntry* e = (VisEntry*)malloc(sizeof(VisEntry));
|
||||
if (!e) { fprintf(stderr, "Out of memory\n"); exit(1); }
|
||||
e->a = a; e->b = b; e->c = c;
|
||||
e->next = hash_table[h];
|
||||
hash_table[h] = e;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static Node* q_front = NULL;
|
||||
static Node* q_back = NULL;
|
||||
|
||||
static void enqueue(Node* n) {
|
||||
n->next = NULL;
|
||||
if (!q_back) q_front = q_back = n;
|
||||
else {
|
||||
q_back->next = n;
|
||||
q_back = n;
|
||||
}
|
||||
}
|
||||
|
||||
static Node* dequeue() {
|
||||
if (!q_front) return NULL;
|
||||
Node* r = q_front;
|
||||
q_front = q_front->next;
|
||||
if (!q_front) q_back = NULL;
|
||||
r->next = NULL;
|
||||
return r;
|
||||
}
|
||||
|
||||
void drain_queue() {
|
||||
Node* n;
|
||||
while ((n = dequeue()) != NULL) free(n);
|
||||
}
|
||||
|
||||
static int safe_val(int128_t x) {
|
||||
if (x > LIMIT || x < -LIMIT) return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
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*)malloc(sizeof(Node));
|
||||
if (!n) { fprintf(stderr, "Out of memory\n"); exit(1); }
|
||||
n->a = na; n->b = nb; n->c = nc;
|
||||
n->move = move;
|
||||
n->parent = curr;
|
||||
n->next = NULL;
|
||||
enqueue(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void try_push_children(Node* curr) {
|
||||
char last = curr->move;
|
||||
|
||||
// Root node doesn't have a 'last' move, so try all three.
|
||||
if (last == '\0') {
|
||||
make_move('A', curr);
|
||||
make_move('B', curr);
|
||||
make_move('C', curr);
|
||||
return;
|
||||
}
|
||||
|
||||
// To avoid AAAA, BBBB, or CCCC, we only pick from the other two moves.
|
||||
// We also prioritize B and C to favor the alternating CBCB pattern.
|
||||
if (last == 'A') {
|
||||
// Just did A: try C then B (the two non-A moves)
|
||||
make_move('C', curr);
|
||||
make_move('B', curr);
|
||||
}
|
||||
else if (last == 'B') {
|
||||
// Just did B: try C (alternating) then A (neutral)
|
||||
// We skip B because it would just take us back to the parent.
|
||||
make_move('C', curr);
|
||||
make_move('A', curr);
|
||||
}
|
||||
else if (last == 'C') {
|
||||
// Just did C: try B (alternating) then A (neutral)
|
||||
// We skip C because it's redundant.
|
||||
make_move('B', curr);
|
||||
make_move('A', curr);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int mode = 0, print_mode = 0;
|
||||
int128_t a0 = 10, b0 = 6, c0 = 0, sum = 0;
|
||||
while (mode == 0) {
|
||||
if (printf("1: manual\n") < 0) return 1;
|
||||
if (printf("2: auto\n") < 0) return 1;
|
||||
if (printf("Choose what mode you want to enter: ") < 0) return 1;
|
||||
if (scanf(" %i", &mode) != 1) {
|
||||
fprintf(stderr, "Input error.\n");
|
||||
return 1;
|
||||
}
|
||||
if (mode != 1 && mode != 2) mode = 0;
|
||||
}
|
||||
|
||||
while (print_mode == 0) {
|
||||
if (printf("1: Print full?\n") < 0) return 1;
|
||||
if (printf("2: Print minimal?\n") < 0) return 1;
|
||||
if (printf("Choose what mode you want to enter: ") < 0) return 1;
|
||||
if (scanf(" %i", &print_mode) != 1) {
|
||||
fprintf(stderr, "Input error.\n");
|
||||
return 1;
|
||||
}
|
||||
if (print_mode != 1 && print_mode != 2) print_mode = 0;
|
||||
}
|
||||
|
||||
if (mode == 1) {
|
||||
if (read_int128_triplet("Enter initial a b c:", &a0, &b0, &c0) != 0) {
|
||||
fprintf(stderr, "Input error.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (a0 == 0 || b0 == 0 || c0 == 0) {
|
||||
printf("Already zero present: (");
|
||||
print_int128(a0); printf(", ");
|
||||
print_int128(b0); printf(", ");
|
||||
print_int128(c0); printf(")\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int128_t greater = b0 >= c0 ? b0 : c0;
|
||||
LIMIT = a0 >= greater ? a0 * greater / 2 : b0 * c0 / 2;
|
||||
|
||||
if (!safe_val(a0) || !safe_val(b0) || !safe_val(c0)) {
|
||||
fprintf(stderr, "Initial values exceed safe limit.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
run_bfs(&sum, a0, b0, c0, print_mode);
|
||||
} else if (mode == 2) {
|
||||
int p = 3;
|
||||
int exponents[3] = {2*p + 2, p, p};
|
||||
int128_t values[3] = {4 * power(a0, p) * power(b0, p), power(a0, p), power(b0, p)};
|
||||
generate_factor_pairs(&sum, 0, exponents, values, 1, print_mode);
|
||||
|
||||
printf("Total sum is: ");
|
||||
print_int128(sum); printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int run_bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode) {
|
||||
drain_queue();
|
||||
clear_hash_table();
|
||||
|
||||
Node* root = malloc(sizeof(Node));
|
||||
if (!root) { fprintf(stderr, "Out of memory\n"); return 0; }
|
||||
root->a = a0; root->b = b0; root->c = c0;
|
||||
root->move = '\0'; root->parent = NULL; root->next = NULL;
|
||||
enqueue(root);
|
||||
visit_and_mark(a0, b0, c0);
|
||||
|
||||
Node* found = NULL;
|
||||
while ((root = dequeue()) != NULL) {
|
||||
if (root->a == 0 || root->b == 0 || root->c == 0) { found = root; break; }
|
||||
try_push_children(root);
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
printf("No solution found within bounds/search limits.\n");
|
||||
drain_queue();
|
||||
clear_hash_table();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int max_moves = 0;
|
||||
for (Node* p = found; p && p->move != '\0'; p = p->parent) max_moves++;
|
||||
|
||||
char* moves;
|
||||
if (print_mode == 1) {
|
||||
moves = malloc(max_moves + 1);
|
||||
if (!moves) { fprintf(stderr, "Out of memory\n"); return 0; }
|
||||
moves[max_moves] = '\0';
|
||||
|
||||
Node* p = found;
|
||||
int idx = max_moves - 1;
|
||||
while (p && p->move != '\0') {
|
||||
moves[idx--] = p->move;
|
||||
p = p->parent;
|
||||
}
|
||||
|
||||
printf("\nSolution found!\n");
|
||||
printf("Initial: (");
|
||||
print_int128(a0); printf(", ");
|
||||
print_int128(b0); printf(", ");
|
||||
print_int128(c0); printf(")\n");
|
||||
|
||||
printf("Final: (");
|
||||
print_int128(found->a); printf(", ");
|
||||
print_int128(found->b); printf(", ");
|
||||
print_int128(found->c); printf(")\n");
|
||||
|
||||
printf("Moves (%d): %s\n", max_moves, moves);
|
||||
} else if (print_mode == 2) {
|
||||
print_int128(c0);
|
||||
printf(" the required amount of moves is: %d\n", max_moves);
|
||||
}
|
||||
*sum+=max_moves;
|
||||
|
||||
if (print_mode == 1) free(moves);
|
||||
drain_queue();
|
||||
clear_hash_table();
|
||||
|
||||
return 1;
|
||||
}
|
||||
Reference in new issue
Block a user