-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheuler93.hs
28 lines (25 loc) · 1002 Bytes
/
euler93.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import Data.Function
import Data.List
targets :: [a -> a -> a] -> [a] -> [a]
targets ops nums = do
-- opOrder <- concatMap permutations $ multicombinations (length nums - 1) ops
opOrder <- sequence . replicate (length nums - 1) $ ops
numOrder <- permutations nums
return $ target opOrder numOrder
where
target ops nums = foldr ($) (last nums) $ zipWith ($) ops (init nums)
ops = [(+),(*),(-),flip (-),safeDiv,flip safeDiv]
where
safeDiv x y = if y == 0
then 0
else x / y
main = do
print $ maximumBy (compare `on` snd) $ map targetLength allForms
where
allForms = do d <- [3..9]
c <- [2..d - 1]
b <- [1..c - 1]
a <- [0..b - 1]
return ((a,b,c,d), sort . nub . filter (>0) . filter isWhole $ targets ops [a,b,c,d])
isWhole x = (fromInteger . truncate) x == x
targetLength (nums, targets) = (nums, length . takeWhile id . zipWith (==) [1..] $ targets)