Created
April 2, 2020 14:41
-
-
Save crclark96/759559132781c96ee40b695646346251 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
➜ runhaskell brokenCalc.hs | |
3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment