24 game/Solve: Difference between revisions

Content added Content deleted
Line 2,066: Line 2,066:
eval (Op op x y) = lookup op ops <*> eval x <*> eval y
eval (Op op x y) = lookup op ops <*> eval x <*> eval y



split :: [Int] -> [([Int],[Int])]
split xs = [(ys, xs \\ ys) | n <- [1..length xs - 1], ys <- select n xs]


select :: Int -> [Int] -> [[Int]]
select :: Int -> [Int] -> [[Int]]
Line 2,077: Line 2,074:
]
]


split :: [Int] -> [([Int],[Int])]
split xs = [(ys, xs \\ ys) | n <- [1..length xs - 1]
, ys <- nub . sort $ select n xs
]


main = mapM_ putStrLn
main = mapM_ (putStrLn . render . toDoc) $ solve 24 [4,6,9,9]</lang>
. nub . sort . map (render . toDoc)
$ solve 24 [3,2,8,9]</lang>
{{Output}}
{{Output}}
<pre>(((2 + 9) / 3) * 8)
<pre>((8 / 2) * (9 - 3))
(((2 / 9) + 3) * 8)
((2 / 9) + (3 * 8))
((3 * 8) - (2 / 9))
((8 - (2 / 9)) * 3)
(((2 / 9) + 8) * 3)
(((2 / 9) + 8) * 3)
(((8 + 9) / 2) * 3)
(((8 + 9) / 2) * 3)
(((9 - 3) * 8) / 2)
(((9 - 3) / 2) * 8)
((2 + (8 * 9)) / 3)
((2 + (8 * 9)) / 3)
((2 / 9) + (3 * 8))
((3 * 8) - (2 / 9))
((3 - (2 / 9)) * 8)
((3 - (2 / 9)) * 8)
((8 - (2 / 9)) * 3)
((9 - (2 * 3)) * 8)
((8 / 2) * (9 - 3))
(((2 / 9) + 3) * 8)
((9 - (2 * 3)) * 8)</pre>
(((2 + 9) / 3) * 8)
(((9 - 3) / 2) * 8)
(((9 - 3) * 8) / 2)</pre>


== {{header|Icon}} and {{header|Unicon}} ==
== {{header|Icon}} and {{header|Unicon}} ==