Created
May 3, 2011 19:05
-
-
Save guitsaru/953983 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
stateString :: Int -> Bool -> String | |
stateString i False = "Case #" ++ show i ++ ": OFF" | |
stateString i True = "Case #" ++ show i ++ ": ON" | |
isOn :: Int -> Int -> Bool | |
isOn snappers snaps = isInt ((fromIntegral (snaps + 1) :: Double) / (fromIntegral (2^snappers) :: Double)) | |
where isInt :: Double -> Bool | |
isInt d = (d - fromIntegral (floor d) :: Double) == 0 | |
readInput :: String -> [(Int, Int)] | |
readInput input = map arrayToIntegerTuple $ tail $ map words $ lines input | |
where arrayToIntegerTuple :: [String] -> (Int, Int) | |
arrayToIntegerTuple [x, y] = (stringToInteger x, stringToInteger y) | |
stringToInteger :: String -> Int | |
stringToInteger s = read s | |
generateOutput :: [(Int, Int)] -> String | |
generateOutput input = unlines $ outputString input 1 | |
where outputString :: [(Int, Int)] -> Int -> [String] | |
outputString (x:xs) i = (stateString i (isOn (fst x) (snd x))):(outputString xs (i + 1)) | |
outputString [] i = [] | |
main = do | |
input <- readFile "large.txt" | |
writeFile "output.txt" (generateOutput (readInput input)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment