Had some weirdness with haskell Set type where unions didn't behave as expected, so I was converting back and forth between Set and List to achieve a full union. But anyways, seems to work
import qualified Data.Set as S
import Numeric (showOct)
type Button = Int -> Int
append :: Int -> Button
append a n = read $ (show n) ++ show a
multiply :: Int -> Button
multiply a n = a * n
divide :: Int -> Button
divide a n = n `div` a
replace :: Int -> Int -> Button
replace a b n = read $ concat $ map (\c -> if [c] == (show a) then show b else [c]) (show n)
oct :: Button
oct n = read $ showOct n ""
bksp :: Button
bksp = read . ('0' :) . reverse . drop 1 . reverse . show
limit :: Button -> Button
limit b n | n' > 9999 = n
| otherwise = n'
where
n' = b n
-- [*9] [0->3] [/3] [*5] [5] [oct] [1->9] [9] [bksp]
buttons :: [Button]
buttons = map (limit) [
multiply 9, replace 0 3, divide 3,
multiply 5, append 5, oct,
replace 1 9, append 9, bksp ]
apply :: Int -> S.Set Int
apply n = S.fromList $ map (\b -> b n) buttons
applyN :: S.Set Int -> S.Set Int
applyN ns = S.fromList $ concat $ map (S.toList . apply) $ S.toList ns
unionN :: [S.Set Int] -> S.Set Int
unionN ss = S.fromList $ concat $ map (S.toList) ss
combinations :: S.Set Int -> S.Set Int -> S.Set Int
combinations acc ns | S.null ns = acc
| otherwise = combinations acc' ns''
where
ns' = applyN ns
acc' = unionN [acc, ns, ns']
ns'' = S.difference ns' acc
main :: IO ()
main = do
let result = sum $ combinations S.empty (S.singleton 0)
putStrLn $ show result
return ()
2
u/Apterygiformes 2h ago
Had some weirdness with haskell Set type where unions didn't behave as expected, so I was converting back and forth between Set and List to achieve a full union. But anyways, seems to work
1
u/Atijohn 1h ago
I love
(<*>)