Files
Le-Compte-est-Bon/lceb.c

115 lines
3.2 KiB
C

/* compte-est-bon.c - should one day solve this game...
*
* $ make compte-est-bon
* $ ./compte-est-bon target n1 n2 [...n6]
*
* At least 2 "n" are mandatory, and no more than 6 are allowed.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <malloc.h>
#include <string.h>
#include "lceb.h"
int main(ac, av)
int ac;
char **av;
{
unsigned target;
STACK inputstack, *stack;
int i, j, k, stacksize, val, res;
//char *ops="+-*/", *opscomb;
//int len_ops=strlen(ops), ncombs, nops;
int ncombs, nstacks, ntrees, nops;
TREE *tree;
int intarray[1024];
int eval;
char *comb;
if (ac < 4 || ac > 2+MAXINPUT) {
fprintf(stderr, "usage: %s target n1 n2 [...n%d]\n", *av, MAXINPUT);
exit(1);
}
target=atoi(av[1]);
stacksize=2*(ac-2)-1;
nops=ac-2-1;
gen_combinations(nops);
//ncombs=ncombinations(len_ops, nops);
ncombs=n_combs();
//print_combs();
printf("target=%d\nstacksize=%d\n", target, stacksize);
set_target(target);
//printf("len_ops=%d\nnops=%d\nops_comb=%d\n", len_ops, nops, ncombs);
//stack=new_stack(stacksize, "Main Stack", 1);
strcpy(inputstack.name, "initial");
for (i=2; i<ac; ++i) {
val=atoi(av[i]);
push_stack(&inputstack, val);
}
//print_stack(stack, 1);
gen_stacks(&inputstack); // printf("sorting stack...\n");
//print_stacks();
//mergesort_stack(stack->stack, 0, stack->last-1);
//print_stack(stack, 1);
/*i=1;
do {
printf("permutation %2d: ", i++);
print_stack(stack, 0);
} while (permute_stack(stack->stack, stack->last));
*/
gen_tree(intarray, nops, 0, 0);
nstacks=n_stacks();
ntrees=n_trees();
printf("stacks=%d\noperators combinations=%d\ntrees=%d\n",
nstacks, ncombs, ntrees);
//for (k=0; k<nstacks; ++k) {
// stack=nth_stack(k);
// printf("%%%%%%%%%%%%%%%%%%\n");
// print_stack(stack, 0);
// printf("%%%%%%%%%%%%%%%%%%\n");
//}
for (i=0; i<ntrees; ++i) {
tree=nth_tree(i);
for (j=0; j<ncombs; ++j) {
comb=nth_comb(j);
for (k=0; k<nstacks; ++k) {
int ncalcs=0;
stack=nth_stack(k);
//printf("%%%%%%%%%%%%%%%%%%\n");
//print_stack(stack, 0);
//printf("%%%%%%%%%%%%%%%%%%\n");
eval=eval_node(tree->head, 0, stack->stack, comb, &ncalcs);
#ifdef DEBUG1
if (eval > 0) {
printf("============================== %d, %d, %d\n", i, j, k);
print_tree(tree, 0);
print_comb(j);
print_stack(stack, 0);
printf("eval=%d - calcs=%d\n", eval, ncalcs);
}
#endif
}
}
//opscomb=combination(ops, nops, i);
//printf("OPS=%s\n", opscomb);
//eval=eval_node(tree->head, 0, stack->stack, nth_comb(0));
//printf("eval=%d\n", eval);
//set_ops_stack(stack, opscomb);
//print_stack(stack, 0);
//res=eval_stack(stack);
//printf("EVAL=%d\n", res);
}
printf("\n");
print_bests();
exit(0);
}