Last active
August 29, 2015 14:24
-
-
Save EugeneLoy/3492a24ef9ee5b879ff3 to your computer and use it in GitHub Desktop.
todo
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
module IO where | |
import System.IO | |
import Control.Exception | |
import Control.DeepSeq | |
import Control.Monad.Trans.Either | |
import Control.Monad.Trans.Class | |
import Data.Either.Combinators | |
import System.Environment | |
failHint :: String -> EitherT SomeException IO a -> EitherT String IO a | |
failHint hint = bimapEitherT (\e -> hint ++ ": " ++ show e) id | |
parseArgs :: EitherT String IO (String, String) | |
parseArgs = do | |
args <- lift getArgs | |
let [inputFile, outputFile] = args | |
failHint "Unable to parse args" $ EitherT $ try $ evaluate $ force (inputFile, outputFile) | |
parseInput :: String -> EitherT String IO (Int, Int, [[Int]]) | |
parseInput content = do | |
let ls = lines content | |
n = (read $ ls !! 0) :: Int | |
logCount = (read $ ls !! 1) :: Int | |
grid = [ map read $ words row | row <- (drop 2 ls) ] | |
failHint "Unable to parse content" $ EitherT $ try $ evaluate $ force (n, logCount, grid) | |
outputGrid :: Handle -> [[Int]] -> IO () | |
outputGrid h grid = hPutStrLn h $ unlines [ unwords $ map show row | row <- grid ] | |
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
module Main where | |
import System.Environment | |
import System.IO | |
import Control.Monad.Trans.Either | |
import Control.Monad.Trans.Class | |
import IO | |
import Solution | |
-- TODO handle file open failure | |
main = do | |
result <- runEitherT (do | |
(inputFile, outputFile) <- parseArgs | |
content <- lift $ readFile inputFile | |
(n, logCount, grid) <- parseInput content | |
lift $ putStrLn "Solving..." | |
let solution = solve1 n logCount grid | |
lift $ outputGrid stdout solution | |
return ()) | |
case result of | |
Right x -> putStrLn "Done" | |
Left message -> hPutStrLn stderr $ "Error: " ++ message | |
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
module Solution where | |
-- |Bruteforce version | |
solve1 :: Int -> Int -> [[Int]] -> [[Int]] | |
solve1 n logCount grid = grid -- TODO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment