Open KnowsCount opened 9 months ago
It's now a recursive backtracking algorithm. I'll put it here in case I forget myself.
backtrack
function: This is the main function that drives the algorithm. It starts with all possible formulae (which initially are just the numbers themselves) and tries to find a formula that evaluates to the target number.
formulate
function: This is a recursive function that tries different combinations of formulae. It first checks if the closest formula to the target number is equal to the target. If it is, it's the solution. If not, it keeps the closest formula as the best solution so far. Then, it generates all possible new formulae by combining the current formulae with different operators, and calls itself recursively on these new formulae.
closestTo
function: This function takes a target number and a list of formulae, and returns the formula that is closest to the target number.
distanceTo
function: This function takes a target number and a formula, and returns the absolute difference between the target number and the evaluated formula.
possibleFormulae
function: This function takes a list of formulae and generates all possible new formulae by combining each pair of formulae with each operator. It takes care to avoid duplications and meaningless operations, like adding zero or multiplying by one.
combine
function: This function takes an operator and two indices, and returns a new list of formulae where the formulae at the given indices are replaced by a new formula that combines them with the given operator.
divisible
function: This function checks if a number is divisible by another number. It's used to avoid generating formulae that involve division by a number that is not a divisor of the other number.
The game freezes for a moment after the last number is generated for the numbers game. This happens because of the fact that the maths solver is a bit slow. I might have to implement it in a different way to fix that.