Is there a way to set up an app to solve equations and then compare them in C++?

Viewed 74

I am trying to write a piece of code for my old Highschool teacher for a game he had us play literally called the "Dice Game." Let's just say that the game takes two d12's and multiplies them together to get a number (D) in this instance. Then you take 3 d6's and roll them to get your A, B, and C variables. You would then either Add, Subtract, Multiply, Divide, Exponentiate, or Root by that number to get as close to as you could to D. Those operations would stand for x and y in the following equation

AxByC=D

I don't know how else to word this, but I am having trouble finding any way to solve these equations and then compare them. Maybe I am missing something simple, but I don't know.

EDIT: I should probably be more clear about the question. I know how to set all the equations up. It is just a matter of finding a way to compare the answers to the D variable and then the other answers to the equation to see which one is closer. The closest number to D wins, thus the whole point to the dice game.

2 Answers

If you are just trying to compare the answers to the D variable, why not loop through each equations result and compare them equal to D?

for (int i = 0; i < equationResults.size(); i++) {
    if (equationResults[i] == D)
        return true;
}

EDIT: If you are trying to find the closest to D, you can compare each answer to D and subtract the answer from D and store it, then return the min value:

closeToD[0] = D - equationResults[0];
return *min_element(closeToD.begin(), closeToD.end());

Since you can juggle the values around, as well as picking operators, you actually have two problems: generating the permutations of variables and generating the permutations of operators. The first part is rather straightforward:

std::array<int, 3> input;
std::sort(input.begin(), input.end());
do {
    compute(input[0], input[1], input[2]);
} while (std::next_permutation(input.begin(), input.end()));

The compute part could be a function that takes such an array of 3 values and finds the best value, or closest to D, or just all values.

Generating all permutations of operators is slightly more annoying because next_permutation can't compare them, and also we accept duplicates. The easiest way is to just brute-force through them; I'll do it just for the slightly easier operators:

std::array<int, 16> compute(int a, int b, int c) {
    return {
       a + b + c,
       a + b - c,
       a + b * c,
       a + b / c,
       a - b + c,
       a - b - c,
       a - b * c,
       a - b / c,
       a * b + c,
       a * b - c,
       a * b * c,
       a * b / c,
       a / b + c,
       a / b - c,
       a / b * c,
       a / b / c,
    };
}

Generating such list of operations programmatically is a bit more challenging; you can't simply do (a op b) op c because of the aforementioned precedence. Doing it this way guarantees that the results are actually achievable because of the operator precedence built into the language.

This will still do redundant computations - e.g. in the first case, the result will be the same regardless of the permutation of a/b/c. Eliminating those is perhaps a more interesting exercise for later. Perhaps a small relief is the fact that if a == b or b == c, next_permutation will already take care of that for us, cutting the number of iterations from 6 to either 3 or 1.

Related