This commit is contained in:
Zander Thannhauser 2025-06-11 08:46:59 -05:00
parent a320cdba33
commit c1351d5ec0

51
main.c
View file

@ -104,6 +104,8 @@ static void parse_args(int argc, char* const* argv)
use_operators.xor = true; use_operators.xor = true;
else if (!strcmp(moving, "nxor")) else if (!strcmp(moving, "nxor"))
use_operators.nxor = true; use_operators.nxor = true;
else if (!strcmp(moving, "ternary"))
use_operators.ternary = true;
else else
{ {
assert(!"TODO"); assert(!"TODO");
@ -342,10 +344,10 @@ void calculate_simplifications(void)
costs[truthtable] = cost; costs[truthtable] = cost;
} }
append(W, 1), lookup[W].kind = ek_W; append(W, 0), lookup[W].kind = ek_W;
append(X, 1), lookup[X].kind = ek_X; append(X, 0), lookup[X].kind = ek_X;
append(Y, 1), lookup[Y].kind = ek_Y; append(Y, 0), lookup[Y].kind = ek_Y;
append(Z, 1), lookup[Z].kind = ek_Z; append(Z, 0), lookup[Z].kind = ek_Z;
append(0, 1), lookup[0].kind = ek_0; append(0, 1), lookup[0].kind = ek_0;
append(M, 1), lookup[M].kind = ek_1; append(M, 1), lookup[M].kind = ek_1;
@ -1146,22 +1148,43 @@ int main(int argc, char* const* argv)
printf("%2i: ", costs[truthtable]), print(truthtable), puts(""); printf("%2i: ", costs[truthtable]), print(truthtable), puts("");
} }
} }
else for (char* line; (line = readline(">>> ")); free(line)) else
{ {
uint16_t truthtable = evaluate(line); int max_cost = 0;
// printf("truthtable = 0b%016b\n", truthtable); for (int i = 0; i < N; i++)
if (costs[truthtable] == INT_MAX)
{ {
puts("unreachable"); int cost = costs[i];
if (cost != INT_MAX && max_cost < cost)
{
max_cost = cost;
}
} }
else
// we subtract 4 from the cost because
puts("");
printf("I can simplify any tree down to %i operators or less.\n",
max_cost);
puts("");
for (char* line; (line = readline(">>> ")); free(line))
{ {
printf("%2i: ", costs[truthtable]), print(truthtable), puts(""); uint16_t truthtable = evaluate(line);
// printf("truthtable = 0b%016b\n", truthtable);
if (costs[truthtable] == INT_MAX)
{
puts("unreachable");
}
else
{
printf("%2i: ", costs[truthtable]), print(truthtable), puts("");
}
} }
} }
return 0; return 0;
} }