Skip to content

Instantly share code, notes, and snippets.

@EugeneLoy
Last active August 29, 2015 14:24
Show Gist options
  • Save EugeneLoy/3492a24ef9ee5b879ff3 to your computer and use it in GitHub Desktop.
Save EugeneLoy/3492a24ef9ee5b879ff3 to your computer and use it in GitHub Desktop.
todo
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 ]
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
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