Skip to content

Instantly share code, notes, and snippets.

@crclark96
Created April 2, 2020 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crclark96/759559132781c96ee40b695646346251 to your computer and use it in GitHub Desktop.
Save crclark96/759559132781c96ee40b695646346251 to your computer and use it in GitHub Desktop.
getHalves :: Int -> [Int]
getHalves = takeWhile (/=0) . iterate (\x -> if x `mod` 2 == 0 then x `div` 2 else 0)
brokenCalc :: Int -> Int -> Int
brokenCalc s t = brokenCalc' 0 s t
brokenCalc' :: Int -> Int -> Int -> Int
brokenCalc' i s t
| s == t = i
| s `elem` halves = brokenCalc' i' (s*2) t
| s > (last halves) = brokenCalc' i' (s-1) t
| otherwise = brokenCalc' i' (s*2) t
where halves = getHalves t
i' = i+1
main = do
print $ brokenCalc 3 10
➜ runhaskell brokenCalc.hs
3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment