Countdown: Difference between revisions
(→{{header|Quorum}}: Management of closest solutions.) |
|||
Line 67: | Line 67: | ||
best = res |
best = res |
||
end |
end |
||
// No remaining operations to be done |
// No remaining operations to be done |
||
if numbers:GetSize() = 0 |
if numbers:GetSize() = 0 |
||
return false |
return false |
||
end |
end |
||
// Initial call only |
// Initial call only |
||
if res not= 0 |
if res not= 0 |
Revision as of 12:44, 6 October 2022
- Task
Given six numbers randomly selected from the list [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 25, 50, 75, 100], calculate using only positive integers and four operations [+, -, *, /] a random number between 101 and 999.
Example:
Using: [3, 6, 25, 50, 75, 100]
Target: 952
Solution:
- 100 + 6 = 106
- 75 * 3 = 225
- 106 * 225 = 23850
- 23850 - 50 = 23800
- 23800 / 25 = 952
- Origins
This is originally a 1972 French television game show. The game consists of randomly selecting six of the twenty-four numbers, from a list of: twenty "small numbers" (two each from 1 to 10), and four "large numbers" of 25, 50, 75 and 100. A random target number between 101 and 999 is generated. The players have 30 seconds to work out a sequence of calculations with the numbers whose final result is as close as possible to the target number. Only the four basic operations: addition, subtraction, multiplication and division can be used to create new numbers and not all six numbers are required. A number can only be used once. Division can only be done if the result has no remainder (fractions are not allowed) and only positive integers can be obtained at any stage of the calculation. (More info on the original game).
- Extra challenge
The brute force algorithm is quite obvious. What is more interesting is to find some optimisation heuristics to reduce the number of calculations. For example, a rather interesting computational challenge is to calculate, as fast as possible, all existing solutions (that means 2'764'800 operations) for all possible games (with all the 13'243 combinations of six numbers out of twenty-four for all 898 possible targets between 101 and 999).
Quorum
use Libraries.Containers.List
use Libraries.Containers.Iterator
use Libraries.System.DateTime
use Libraries.Compute.Math
class Countdown
integer best = 0
action Main
DateTime datetime
number start = datetime:GetEpochTime()
List<integer> numbers
numbers:Add(100)
numbers:Add(75)
numbers:Add(50)
numbers:Add(25)
numbers:Add(6)
numbers:Add(3)
if not Solution(952,0,numbers)
output "Best solution found is " + best
end
number stop = datetime:GetEpochTime()
output stop-start + " ms"
end
action Solution(integer target, integer res, List<integer> numbers) returns boolean
Math math
// Check closest solution
if math:AbsoluteValue(target-best) > math:AbsoluteValue(target-res)
best = res
end
// No remaining operations to be done
if numbers:GetSize() = 0
return false
end
// Initial call only
if res not= 0
numbers:Add(res)
end
Iterator<integer> it0 = numbers:GetIterator()
repeat while it0:HasNext()
integer n0 = it0:Next()
List<integer> numbers1 = cast(List<integer>, numbers:Copy())
numbers1:Remove(n0)
Iterator<integer> it1 = numbers1:GetIterator()
repeat while it1:HasNext()
integer n1 = it1:Next()
List<integer> numbers2 = cast(List<integer>, numbers1:Copy())
numbers2:Remove(n1)
res = n0 + n1
if res = target or Solution(target, res, cast(List<integer>, numbers2:Copy()))
output res + " = " + n0 + " + " + n1
return true
end
res = n0 * n1
if res = target or Solution(target, res, cast(List<integer>, numbers2:Copy()))
output res + " = " + n0 + " * " + n1
return true
end
// Substraction and division are not symetrical operations
if n0 < n1
integer temp = n0
n0 = n1
n1 = temp
end
if n0 not= n1
res = n0 - n1
if res = target or Solution(target, res, cast(List<integer>, numbers2:Copy()))
output res + " = " + n0 + " - " + n1
return true
end
end
if n0 mod n1 = 0
res = n0 / n1
if res = target or Solution(target, res, cast(List<integer>, numbers2:Copy()))
output res + " = " + n0 + " / " + n1
return true
end
end
end
end
return false
end
end
- Output:
952 = 50 + 902 902 = 22550 / 25 22550 = 50 + 22500 22500 = 7500 * 3 7500 = 100 * 75 214.0 ms
Wren
This is based on the original Quorum algorithm as it's more or less the approach I'd have used anyway.
The latest algorithm is not working properly as numbers are being used twice (50 in the first example).
A bit slow but not too bad for Wren :)
import "random" for Random
import "./fmt" for Fmt
var countdown // recursive function
countdown = Fn.new { |numbers, target|
if (numbers.count == 1) return false
for (n0 in numbers) {
var nums1 = numbers.toList
nums1.remove(n0)
for (n1 in nums1) {
var nums2 = nums1.toList
nums2.remove(n1)
var res = n0 + n1
var numsNew = nums2.toList
numsNew.add(res)
if (res == target || countdown.call(numsNew, target)) {
Fmt.print("$d = $d + $d", res, n0, n1)
return true
}
res = n0 * n1
numsNew = nums2.toList
numsNew.add(res)
if (res == target || countdown.call(numsNew, target)) {
Fmt.print("$d = $d * $d", res, n0, n1)
return true
}
if (n0 > n1) {
res = n0 - n1
numsNew = nums2.toList
numsNew.add(res)
if (res == target || countdown.call(numsNew, target)) {
Fmt.print("$d = $d - $d", res, n0, n1)
return true
}
} else if (n1 > n0) {
res = n1 - n0
numsNew = nums2.toList
numsNew.add(res)
if (res == target || countdown.call(numsNew, target)) {
Fmt.print("$d = $d - $d", res, n1, n0)
return true
}
}
if (n0 > n1) {
if (n0 % n1 == 0) {
res = n0 / n1
numsNew = nums2.toList
numsNew.add(res)
if (res == target || countdown.call(numsNew, target)) {
Fmt.print("$d = $d / $d", res, n0, n1)
return true
}
}
} else {
if (n1 % n0 == 0) {
res = n1 / n0
numsNew = nums2.toList
numsNew.add(res)
if (res == target || countdown.call(numsNew, target)) {
Fmt.print("$d = $d / $d", res, n1, n0)
return true
}
}
}
}
}
return false
}
var allNumbers = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 25, 50, 75, 100]
var rand = Random.new()
var numbersList = [
[3, 6, 25, 50, 75, 100],
[100, 75, 50, 25, 6, 3], // see if there's much difference if we reverse the first example
[8, 4, 4, 6, 8, 9],
rand.sample(allNumbers, 6)
]
var targetList = [952, 952, 594, rand.int(101, 1000)]
for (i in 0...numbersList.count) {
System.print("Using : %(numbersList[i])")
System.print("Target: %(targetList[i])")
var start = System.clock
var done = countdown.call(numbersList[i], targetList[i])
System.print("Took %(((System.clock - start) * 1000).round) ms")
if (!done) System.print("No solution exists")
System.print()
}
- Output:
Sample output (as the fourth example is random):
Using : [3, 6, 25, 50, 75, 100] Target: 952 952 = 23800 / 25 23800 = 23850 - 50 23850 = 225 * 106 106 = 6 + 100 225 = 3 * 75 Took 1525 ms Using : [100, 75, 50, 25, 6, 3] Target: 952 952 = 23800 / 25 23800 = 23850 - 50 23850 = 106 * 225 225 = 75 * 3 106 = 100 + 6 Took 1522 ms Using : [8, 4, 4, 6, 8, 9] Target: 594 594 = 54 * 11 11 = 8 + 3 54 = 6 * 9 3 = 12 / 4 12 = 8 + 4 Took 27 ms Using : [2, 4, 9, 10, 3, 5] Target: 363 363 = 3 + 360 360 = 9 * 40 40 = 10 + 30 30 = 5 * 6 6 = 2 + 4 Took 107 ms