Solves the countdown problem.
Given a set S
of natural numbers, combine these using +
, -
, /
, *
and ()
to get a desired result r
. The brackets are required to distinguish between otherwise identitical combinations, e.g.:
Some useful resources on the topic:
- Writing an algorithm to decide whether a target number can be reached with a set
- Writing an algorithm to decide whether a target number can be reached with a set of other numbers and specific operators?
- Solver written in C++
It seems brute force is the only way of doing this, i.e. try as many combinations in 30 seconds and see which one is closest to the desired result. This algorithm could be desribed as follows:
- Set
A = [(,n) for n in S]
to be the set of available numbers and how they were obtained (order of operations). - For
(a1,a1)
inunique(product(A,A))
, combinea1
anda1
using+
,-
,/
,*
and()
. - If this combination is equal to
r
, then stop. Else, save this number and the associated operations with it inA_new
. - Set
A = union(A,A_new)
. - Go back to step 2.
The problem with this algorithm is that some impossible combinations are produced, e.g. if S = [2,3,10,25]
then a possible combination (in terms of the algorithm) is 2 * 3 * 2
, even though 2 only appears once. I can get over this by saving the numbers used as well as the order of operation in the set A
.
Just do include(<path_to_CountdownSolver.jl>)
before running a script or a test. Similarly for the functions to build the web UI.
Alternatively:
julia> ENV["JULIA_PKG_DEVDIR"] = joinpath(ENV["HOME"], "Desktop", "Julia_Dev")
(@v1.5) pkg> add git@github.com:junglegobs/CountdownSolver.jl.git
(@v1.5) pkg> dev CountdownSolver # Will copy files to Desktop/Julia_Dev
julia> using Revise
(@v1.5) pkg> activate CountdownSolver
julia> using CountdownSolver
The package is now located at joinpath(ENV["HOME"], "Desktop", "Julia_Dev")
. Any exported functions from the module CountdownSolver
are now accessible, and the Revise.jl
package ensures that any changes are automatically taken into account. By activating CountdownSolver
, any packages will be added to the CountdownSolver.jl
package (and not your main environment).