#| 24 game in Racket/Gamble From Rosetta Code http://rosettacode.org/wiki/24_game/Solve """ Write a function that given four digits subject to the rules of the 24 game, computes an expression to solve the game if possible. Show examples of solutions generated by the function """ Here's the solutions for '(1 2 3 4) and target 24: numbers: (1 2 3 4) target: 24 (model (1 2 3 4) 24) variable : res ((2 * 3 = 6) (6 * 4 = 24) (24 / 1 = 24)): 1/48 (0.020833333333333332) ((1 + 2 = 3) (3 + 3 = 6) (6 * 4 = 24)): 1/48 (0.020833333333333332) ((4 * 2 = 8) (8 * 3 = 24) (24 * 1 = 24)): 1/48 (0.020833333333333332) ((2 * 1 = 2) (2 * 4 = 8) (8 * 3 = 24)): 1/48 (0.020833333333333332) ((4 * 2 = 8) (8 / 1 = 8) (8 * 3 = 24)): 1/48 (0.020833333333333332) ((2 * 4 = 8) (8 * 1 = 8) (8 * 3 = 24)): 1/48 (0.020833333333333332) ((1 * 4 = 4) (4 * 2 = 8) (8 * 3 = 24)): 1/48 (0.020833333333333332) ((1 * 2 = 2) (2 * 4 = 8) (8 * 3 = 24)): 1/48 (0.020833333333333332) ((2 / 1 = 2) (2 * 4 = 8) (8 * 3 = 24)): 1/48 (0.020833333333333332) ((4 / 1 = 4) (4 * 3 = 12) (12 * 2 = 24)): 1/48 (0.020833333333333332) ((4 * 3 = 12) (12 * 2 = 24) (24 * 1 = 24)): 1/48 (0.020833333333333332) ((1 * 2 = 2) (2 * 3 = 6) (6 * 4 = 24)): 1/48 (0.020833333333333332) ((2 + 1 = 3) (3 + 3 = 6) (6 * 4 = 24)): 1/48 (0.020833333333333332) ((4 * 3 = 12) (12 * 1 = 12) (12 * 2 = 24)): 1/48 (0.020833333333333332) ((2 * 4 = 8) (8 * 3 = 24) (24 / 1 = 24)): 1/48 (0.020833333333333332) ((4 * 2 = 8) (8 * 1 = 8) (8 * 3 = 24)): 1/48 (0.020833333333333332) ((2 / 1 = 2) (2 * 3 = 6) (6 * 4 = 24)): 1/48 (0.020833333333333332) ((3 / 1 = 3) (3 * 2 = 6) (6 * 4 = 24)): 1/48 (0.020833333333333332) ((2 * 4 = 8) (8 * 3 = 24) (24 * 1 = 24)): 1/48 (0.020833333333333332) ((4 * 3 = 12) (12 / 1 = 12) (12 * 2 = 24)): 1/48 (0.020833333333333332) ((4 / 1 = 4) (4 * 2 = 8) (8 * 3 = 24)): 1/48 (0.020833333333333332) ((1 * 4 = 4) (4 * 3 = 12) (12 * 2 = 24)): 1/48 (0.020833333333333332) ((4 * 1 = 4) (4 * 2 = 8) (8 * 3 = 24)): 1/48 (0.020833333333333332) ((3 * 2 = 6) (6 * 4 = 24) (24 * 1 = 24)): 1/48 (0.020833333333333332) ((3 * 4 = 12) (12 * 2 = 24) (24 * 1 = 24)): 1/48 (0.020833333333333332) ((4 * 3 = 12) (12 * 2 = 24) (24 / 1 = 24)): 1/48 (0.020833333333333332) ((2 * 3 = 6) (6 / 1 = 6) (6 * 4 = 24)): 1/48 (0.020833333333333332) ((1 * 3 = 3) (3 * 2 = 6) (6 * 4 = 24)): 1/48 (0.020833333333333332) ((2 * 1 = 2) (2 * 3 = 6) (6 * 4 = 24)): 1/48 (0.020833333333333332) ((3 * 2 = 6) (6 / 1 = 6) (6 * 4 = 24)): 1/48 (0.020833333333333332) ((4 * 2 = 8) (8 * 3 = 24) (24 / 1 = 24)): 1/48 (0.020833333333333332) ((3 + 2 = 5) (5 + 1 = 6) (6 * 4 = 24)): 1/48 (0.020833333333333332) ((3 * 4 = 12) (12 * 2 = 24) (24 / 1 = 24)): 1/48 (0.020833333333333332) ((2 * 3 = 6) (6 * 1 = 6) (6 * 4 = 24)): 1/48 (0.020833333333333332) ((3 * 4 = 12) (12 / 1 = 12) (12 * 2 = 24)): 1/48 (0.020833333333333332) ((4 * 1 = 4) (4 * 3 = 12) (12 * 2 = 24)): 1/48 (0.020833333333333332) ((3 / 1 = 3) (3 * 4 = 12) (12 * 2 = 24)): 1/48 (0.020833333333333332) ((2 * 4 = 8) (8 / 1 = 8) (8 * 3 = 24)): 1/48 (0.020833333333333332) ((3 * 4 = 12) (12 * 1 = 12) (12 * 2 = 24)): 1/48 (0.020833333333333332) ((3 * 1 = 3) (3 * 4 = 12) (12 * 2 = 24)): 1/48 (0.020833333333333332) ((3 + 1 = 4) (4 + 2 = 6) (6 * 4 = 24)): 1/48 (0.020833333333333332) ((2 * 3 = 6) (6 * 4 = 24) (24 * 1 = 24)): 1/48 (0.020833333333333332) ((1 * 3 = 3) (3 * 4 = 12) (12 * 2 = 24)): 1/48 (0.020833333333333332) ((3 * 2 = 6) (6 * 4 = 24) (24 / 1 = 24)): 1/48 (0.020833333333333332) ((1 + 3 = 4) (4 + 2 = 6) (6 * 4 = 24)): 1/48 (0.020833333333333332) ((2 + 3 = 5) (5 + 1 = 6) (6 * 4 = 24)): 1/48 (0.020833333333333332) ((3 * 1 = 3) (3 * 2 = 6) (6 * 4 = 24)): 1/48 (0.020833333333333332) ((3 * 2 = 6) (6 * 1 = 6) (6 * 4 = 24)): 1/48 (0.020833333333333332) If we also include expt (**) then there some extra solutions: ((3 * 4 = 12) (12 ** 1 = 12) (12 * 2 = 24)): 1/66 (0.015151515151515152) ((4 * 3 = 12) (12 ** 1 = 12) (12 * 2 = 24)): 1/66 (0.015151515151515152) ((2 * 4 = 8) (8 ** 1 = 8) (8 * 3 = 24)): 1/66 (0.015151515151515152) ((3 ** 1 = 3) (3 * 2 = 6) (6 * 4 = 24)): 1/66 (0.015151515151515152) ((2 ** 1 = 2) (2 * 3 = 6) (6 * 4 = 24)): 1/66 (0.015151515151515152) ((2 * 3 = 6) (6 ** 1 = 6) (6 * 4 = 24)): 1/66 (0.015151515151515152) ((2 ** 1 = 2) (2 * 4 = 8) (8 * 3 = 24)): 1/66 (0.015151515151515152) ((3 ** 1 = 3) (3 * 4 = 12) (12 * 2 = 24)): 1/66 (0.015151515151515152) ((3 * 4 = 12) (12 * 2 = 24) (24 ** 1 = 24)): 1/66 (0.015151515151515152) ((2 * 4 = 8) (8 * 3 = 24) (24 ** 1 = 24)): 1/66 (0.015151515151515152) ((4 ** 1 = 4) (4 * 2 = 8) (8 * 3 = 24)): 1/66 (0.015151515151515152) ((3 * 2 = 6) (6 ** 1 = 6) (6 * 4 = 24)): 1/66 (0.015151515151515152) ((3 * 2 = 6) (6 * 4 = 24) (24 ** 1 = 24)): 1/66 (0.015151515151515152) ((4 * 3 = 12) (12 * 2 = 24) (24 ** 1 = 24)): 1/66 (0.015151515151515152) ((4 * 2 = 8) (8 * 3 = 24) (24 ** 1 = 24)): 1/66 (0.015151515151515152) ((4 * 2 = 8) (8 ** 1 = 8) (8 * 3 = 24)): 1/66 (0.015151515151515152) ((4 ** 1 = 4) (4 * 3 = 12) (12 * 2 = 24)): 1/66 (0.015151515151515152) ((2 * 3 = 6) (6 * 4 = 24) (24 ** 1 = 24)): 1/66 (0.015151515151515152) This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Racket page: http://www.hakank.org/racket/ |# #lang gamble (require racket) (require "gamble_utils.rkt") ; (require "gamble_distributions.rkt") (define (model numbers target) (show2 "model" numbers target) (enumerate ; The available functions (define ops (list + - * / ; expt )) (define (show-eq op a b) (format "~a ~a ~a =" a (conv-op op) b)) (define (conv-op op) (cond [(eq? op +) "+"] [(eq? op *) "*"] [(eq? op -) "-"] [(eq? op /) "/"] ; [(eq? op expt) "**"] )) (define (f target) (define (loopx lst nums) (if (or (empty? nums)) lst (let* ([val (last (last lst))] [op (uniform-draw ops)] [num (uniform-draw nums)] [res (op val num)]) (loopx (append lst (list (list (show-eq op val num) res))) (remove num nums)) ) )) (let ([init-num (uniform-draw numbers)]) (loopx (list (list init-num)) (remove init-num numbers) ) ) ) (define res (f target)) (define len (length res)) (define last-val (last (last res))) ; Ensure we got the target as a result (observe/fail (= last-val target)) (list (rest res)) ) ) (define unsolvable '()) ; (define numbers (for/list ([i 4]) (add1 (random-integer 9)))) (define numbers '(1 2 3 4)) ; (define numbers '(1 2 3 4 5)) ; (for ([target (range 24 25)]) (for ([target (range 0 25)]) (newline) (displayln (format "numbers: ~a target: ~a" numbers target)) (with-handlers ([exn:fail? (lambda (exn) (displayln "no solution") (set! unsolvable (append unsolvable (list (list target)) )))]) (show-marginals (model numbers target) (list "res" ) ) )) (newline) (show "unsolvable" unsolvable)