# How would you code a countdown solver?

How would you code a countdown solver?

You have 30 seconds to reach the target number using 6 numbers and the four mathematics operations..

You don't have to use all numbers.

Players can select small numbers (1-10) or larger number (25, 50, 75, 100).

1. 2 weeks ago
Anonymous

I wrote one when learning C
https://paste.debian.net/1280875/

• 2 weeks ago
Anonymous

• 2 weeks ago
Anonymous

I'm a hobbyist
>This is hard to read
it tries every possible RPN expression with given numbers and four arithmetic operations and prints the shortest that evaluates to the target number
there is no faster algorithm to solve those questions other than perhaps storing the answer to every possible question and looking up the one asked

2. 2 weeks ago
Anonymous

God already coded my dick to dick her.

3. 2 weeks ago
Anonymous

What a dumbass boring show. Is this what british "people" really watch for entertainment?

4. 2 weeks ago
Anonymous

RAPE DA JOOS !!

5. 2 weeks ago
Anonymous

I'm too thick to come with my own, but there's an example in Programming in Haskell. (1/2)
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}

module Countdown (Result, countdown) where

import Control.DeepSeq (NFData)
import GHC.Generics (Generic)

data Expr' = Val' Int | App Op Expr' Expr' deriving (Generic, NFData)

instance Show Expr' where
show (Val' n) = show n
show (App o l r) = let
brak (Val' x) = show x
brak e = "(" ++ show e ++ ")"
in brak l ++ " " ++ show o ++ " " ++ brak r

instance Num Expr' where
(*) = App Mul
(+) = App Add'
(-) = App Sub
abs = id
signum = const (Val' 1)
fromInteger = Val' . fromIntegral

instance Eq Expr' where
Val' x == Val' y = x == y
(App op l r) == (App op' l' r') = op == op' && l == l' && r == r'
_ == _ = False

-- 6 c)
instance Ord Expr' where
ap <= ap' = depth ap <= depth ap'
where depth :: Expr' -> Int
depth (Val' _) = 0
depth (App _ l r) = 1 + depth l + depth r

data Op = Add' | Sub | Mul | Div deriving (Eq, Generic, NFData)

instance Show Op where
show Add' = "+"
show Sub = "-"
show Div = "/"
show Mul = "*"

valid :: Op -> Int -> Int -> Bool
valid Add' _ _ = True
valid Mul _ _ = True
valid Sub x y = x > y
valid Div x y = x `mod` y == 0

applyOp :: Op -> Int -> Int -> Int
applyOp Add' = (+)
applyOp Sub = (-)
applyOp Mul = (*)
applyOp Div = div

subs :: [a] -> [[a]]
subs [] = [[]]
subs (x:xs) = yss ++ map (x:) yss where yss = subs xs

interleave :: a -> [a] -> [[a]]
interleave x [] = [[x]]
interleave x (y:ys) = (x:y:ys) : map (y:) (interleave x ys)

perms :: [a] -> [[a]]
perms [] = [[]]
perms (x:xs) = concatMap (interleave x) (perms xs)

choices :: [a] -> [[a]]
choices = concatMap perms . subs

• 2 weeks ago
Anonymous

(2/2)
splitLists :: [a] -> [([a], [a])]
splitLists [] = []
splitLists [_] = []
splitLists (x:xs) = ([x], xs) : [(x:ls, rs) | (ls,rs) <- splitLists xs]

type Result = (Expr', Int)

results :: [Int] -> [Result]
results [] = []
results [n] = [(Val' n, n) | n > 0]
results ns = [res | (ls, rs) <- splitLists ns,
lx <- results ls,
ry <- results rs,
res <- combine' lx ry]

combine' :: Result -> Result -> [Result]
combine' (l,x) (r,y) = [(App o l r, applyOp o x y) | o <- ops, valid o x y]
where ops = [Add', Sub, Div, Mul]

countdown :: [Int] -> Int -> [Expr']
countdown ns n = [e | ns' <- choices ns, (e,m) <- results ns', m == n]

6. 2 weeks ago
Anonymous

Maybe I misunderstand the show based on your screenshot, but if it's always 3 digits numbers can't it be solved every time by (hundreds place x 100)+(tens place x 10)+(ones place)?

7. 2 weeks ago
Anonymous

sirs, good morning. That is a nice tnetennba

8. 2 weeks ago
PyDick

alright then, in python:

from itertools import permutations, combinations, product

def countdown_solver(numbers, target):
operations = ['+', '-', '*', '/']
for n in range(1, len(numbers)+1):
for c in combinations(numbers, n):
for p in permutations(c):
for op in product(operations, repeat=n-1):
equation = ''.join([str(p[i])+op[i] for i in range(n-1)] + [str(p[-1])])
try:
if eval(equation) == target:
return equation
except ZeroDivisionError:
pass
return 'No solution found'

numbers = [100, 75, 50, 25, 10, 5]
target = 999
print(countdown_solver(numbers, target))

• 2 weeks ago
Anonymous

that's neat

• 2 weeks ago
Anonymous

>[50, 9, 2, 1, 10, 5] 727

• 2 weeks ago
Anonymous

5 727
50 + 2 = 52
9 + 5 = 14
52 * 14 = 728
728 - 1 = 727

• 2 weeks ago
Anonymous

I know, I mean

alright then, in python:

from itertools import permutations, combinations, product

def countdown_solver(numbers, target):
operations = ['+', '-', '*', '/']
for n in range(1, len(numbers)+1):
for c in combinations(numbers, n):
for p in permutations(c):
for op in product(operations, repeat=n-1):
equation = ''.join([str(p[i])+op[i] for i in range(n-1)] + [str(p[-1])])
try:
if eval(equation) == target:
return equation
except ZeroDivisionError:
pass
return 'No solution found'

numbers = [100, 75, 50, 25, 10, 5]
target = 999
print(countdown_solver(numbers, target))

can't solve it

It's slow as shit and inefficient, but it works.

from random import choice as ch

def solveCountdown(result: int, numbers: list) -> str:
symbols = ["+", "-", "*", "/"]
if result in numbers:
return result
for n in range(2, len(numbers)):
count = 0
for _ in range(10000):
eq = ""
for number in range(n - 1):
eq += str(ch(numbers)) + ch(symbols)
eq += str(ch(numbers))
if eval(eq) == result:
return eq

print(solveCountdown(467, [100, 50, 75, 3, 9, 8]))

fails on OP

• 2 weeks ago
Anonymous
• 2 weeks ago
Anonymous

Pseudo code doesn't have imports (aside from the bare bones stdlibs). The whole point of it is to show the logic of a program.

9. 2 weeks ago
Anonymous

What do you mean select numbers?
If you can choose any number like that it's trivial.
Surely you have to make do with random numbers like in the screenshot. (100 * (5+2+1) - 10 + 4) = 794

• 2 weeks ago
Anonymous

not him but I think he meant each instances of this game have a set of 6 numbers taken from the numbers 1 to 10 and from the numbers 25, 50, 75, 100.
That way it is realistically solvable by an average man, else it would be too hard.

10. 2 weeks ago
Anonymous

It's slow as shit and inefficient, but it works.

from random import choice as ch

def solveCountdown(result: int, numbers: list) -> str:
symbols = ["+", "-", "*", "/"]
if result in numbers:
return result
for n in range(2, len(numbers)):
count = 0
for _ in range(10000):
eq = ""
for number in range(n - 1):
eq += str(ch(numbers)) + ch(symbols)
eq += str(ch(numbers))
if eval(eq) == result:
return eq

print(solveCountdown(467, [100, 50, 75, 3, 9, 8]))