From 7d3518f47401ee98f77747495806aff223d07ebc Mon Sep 17 00:00:00 2001 From: Andrea Bellandi Date: Sun, 9 Aug 2015 01:34:18 +0200 Subject: [PATCH] first ReadString Version --- src/JSONDeser.hs | 2 +- src/Opt.hs | 24 +++++++++++++++--------- src/PowerPhrases.hs | 2 +- src/ReadString.hs | 42 ++++++++++++++++++++++-------------------- src/SinglePasses.hs | 28 ++++++++++++++++++++-------- 5 files changed, 59 insertions(+), 39 deletions(-) diff --git a/src/JSONDeser.hs b/src/JSONDeser.hs index 36d874c..10e28c2 100644 --- a/src/JSONDeser.hs +++ b/src/JSONDeser.hs @@ -1,4 +1,4 @@ -{-# LANGUAGE OverloadedStrings, DeriveGeneric #-} +{-# LANGUAGE DeriveGeneric #-} module JSONDeser(readInput) where import qualified Data.Set as Set diff --git a/src/Opt.hs b/src/Opt.hs index 1537293..58b38dd 100644 --- a/src/Opt.hs +++ b/src/Opt.hs @@ -11,17 +11,18 @@ data Flag = File String | PowerPhrase String deriving Show -data Options = Options { optFile :: [String] - , optTime :: Maybe Int - , optMem :: Maybe Int - , optPowerPhrase :: Maybe String - } +data Options = Options { optFile :: [String], + optTime :: Maybe Int, + optMem :: Maybe Int, + optPowerPhrase :: Maybe String, + optSeedNumber :: Int } deriving Show -startOptions = Options { optFile = [] - , optTime = Nothing - , optMem = Nothing - , optPowerPhrase = Nothing +startOptions = Options { optFile = [], + optTime = Nothing, + optMem = Nothing, + optPowerPhrase = Nothing, + optSeedNumber = 0 } options :: [ OptDescr (Options -> IO Options) ] @@ -45,6 +46,11 @@ options = [ Option "f" ["filename"] (\arg opt -> return opt { optPowerPhrase = Just arg }) "POWERPHRASE") "Power Phrase" + , Option "n" ["seednumber"] + (ReqArg + (\arg opt -> return opt { optSeedNumber = (read arg) }) + "SEEDNUMBER") + "Seed Number" ] parseArgs :: [String] -> IO Options diff --git a/src/PowerPhrases.hs b/src/PowerPhrases.hs index b74d319..7d9233c 100644 --- a/src/PowerPhrases.hs +++ b/src/PowerPhrases.hs @@ -1,7 +1,7 @@ module PowerPhrases where import Data.Maybe -import Datatypes +import VM charToCommand :: Char -> Maybe Command diff --git a/src/ReadString.hs b/src/ReadString.hs index fa17cef..475434e 100644 --- a/src/ReadString.hs +++ b/src/ReadString.hs @@ -5,33 +5,35 @@ import GHC.Generics import Data.Aeson import Data.Aeson.Types +import Data.Maybe import System.Environment -import qualified Data.ByteString.Lazy as BS +import qualified Data.ByteString.Lazy.Char8 as BS import System.IO -import Datatypes +import qualified Datatypes as DT +import qualified VM import Opt import JSONDeser -import VM (Command, cmdToString) - - -data JSONSer = JSONSer { problemId :: Int, - seed :: Int, - tag :: String, - solution :: String - } deriving (Show, Generic) - -instance FromJSON JSONSer -instance ToJSON JSONSer +import SinglePasses +import PowerPhrases main :: IO () main = do args <- getArgs opt <- parseArgs args - file <- return ((optFile opt) !! 0) - str <- BS.readFile file + filename <- return ((optFile opt) !! 0) + file <- BS.readFile filename + phrase <- return (fromJust $ optPowerPhrase opt) + seedn <- return $ optSeedNumber opt + (id, seedgame) <- return $ readInput file + (seed,game) <- return $ unzip seedgame + outputs <- return $ passes $ propagateCommand (game !! seedn) phrase + BS.putStrLn $ encode outputs -packAll :: Int -> [Int] -> [[Command]] -> [JSONSer] -packAll id seeds commandLists = zipWith (\x y -> JSONSer id x "lilik0" y) seeds commandStrings +propagateCommand :: DT.Game -> String -> [(DT.Game, VM.Notes)] +propagateCommand game str = (game,VM.OK):(propagateCommand0 game str) where - commandStrings = map cmdToString commandLists - - + propagateCommand0 game0 [] = [] + propagateCommand0 game0 (c:cs) = res1:(propagateCommand game1 cs) + where + res1@(game1,note1) = VM.step game0 (fromJust $ charToCommand c) + + diff --git a/src/SinglePasses.hs b/src/SinglePasses.hs index ee2bec5..7e01c45 100644 --- a/src/SinglePasses.hs +++ b/src/SinglePasses.hs @@ -1,14 +1,19 @@ -{-# LANGUAGE DeriveDataTypeable #-} -module SinglePasses where +{-# LANGUAGE DeriveGeneric #-} +module SinglePasses(Output(..), Cell(..), passes) where import Data.Set(Set(..),difference,union,toList,empty) import Data.Maybe import Data.Typeable +import GHC.Generics +import Data.Aeson +import Data.Aeson.Types + import qualified Datatypes as DT import qualified VM + data Cell = Cell { x :: Int, y :: Int} - deriving (Show, Typeable) + deriving (Show, Generic) data Output = Output { width :: Int, height :: Int, @@ -17,7 +22,14 @@ data Output = Output { width :: Int, unit :: [Cell], score :: Int } - deriving (Show, Typeable) + deriving (Show, Generic) + +instance FromJSON Cell +instance ToJSON Cell + +instance FromJSON Output +instance ToJSON Output + passes :: [(DT.Game, VM.Notes)] -> [Output] passes gamesnotes = map generateoutput (gameswithtouched gamesnotes) @@ -27,7 +39,7 @@ passes gamesnotes = map generateoutput (gameswithtouched gamesnotes) filled = filledcells game, touched = touchedcells touched, unit = unitcells game, - score = DT.points game + score = DT.score game } filledcells gm = extractcell $ DT.filled $ DT.board gm unitcells gm = if null $ DT.units gm @@ -37,17 +49,17 @@ passes gamesnotes = map generateoutput (gameswithtouched gamesnotes) gameswithtouched :: [(DT.Game,VM.Notes)] -> [((DT.Game,VM.Notes),Set DT.Cell)] gameswithtouched [] = [] -gameswithtouched (x:[]) = [(x,empty)] +gameswithtouched (x:[]) = [(x,Data.Set.empty)] gameswithtouched (x:xs) = (x,touched):lastels where touched1 = snd $ head lastels touched2 = if null $ DT.units $ fst $ fst $ head lastels - then empty + then Data.Set.empty else DT.members $ head $ DT.units $ fst $ fst $ head lastels touched = difference (difference (union touched1 touched2) cells) units cells = DT.filled $ DT.board $ fst x units = if null $ DT.units $ fst x - then empty + then Data.Set.empty else DT.members $ head $ DT.units $ fst x touchedcells tc = extractcell touched lastels = gameswithtouched xs