From 9deaefe0fdb8a8c372c56e50fc8b8ad8457dcca0 Mon Sep 17 00:00:00 2001 From: zolfa Date: Sun, 9 Aug 2015 02:25:39 +0200 Subject: [PATCH] add second strategy --- src/Main2.hs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ src/Strategy2.hs | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 src/Main2.hs create mode 100644 src/Strategy2.hs diff --git a/src/Main2.hs b/src/Main2.hs new file mode 100644 index 0000000..5ce727c --- /dev/null +++ b/src/Main2.hs @@ -0,0 +1,50 @@ +{-# LANGUAGE DeriveGeneric #-} +module Main where + +import GHC.Generics +import Data.Aeson +import Data.Aeson.Types + +import System.Environment +import qualified Data.ByteString.Lazy.Char8 as BS +import System.IO +import Datatypes +import Opt +import JSONDeser +import Strategy2 +import VM (Command, cmdToString) + +data JSONSer = JSONSer { problemId :: Int, + seed :: Int, + tag :: String, + solution :: String + } deriving (Show, Generic) + +instance FromJSON JSONSer +instance ToJSON JSONSer + +main :: IO () +main = do args <- getArgs + opt <- parseArgs args + file <- return ((optFile opt) !! 0) + str <- BS.readFile file + (id, gmseed) <- return (readInput str) + commandspoints <- return (map (\(seed,game) -> strat2 game) gmseed) + (commands,points) <- return $ unzip commandspoints + seeds <- return ((map (\(seed, _) -> seed)) gmseed) + writeFile ("scores") (scoredata id (fst $ unzip gmseed) points) + BS.putStrLn $ encode $ (packAll id seeds commands) + + +packAll :: Int -> [Int] -> [[Command]] -> [JSONSer] +packAll id seeds commandLists = zipWith (\x y -> JSONSer id x "lilik1" y) seeds commandStrings + where + commandStrings = map cmdToString commandLists + +scoredata :: Int -> [Int] -> [Int] -> String +scoredata id seeds points = pretty + where + pretty = foldl (\x (a,b,c) -> (show a) ++ " " ++ (show b) ++ " " ++ (show c) ++ "\n" ++ x) "" zipdata + zipdata = zip3 ids seeds points + ids = replicate (length seeds) id + diff --git a/src/Strategy2.hs b/src/Strategy2.hs new file mode 100644 index 0000000..c95631c --- /dev/null +++ b/src/Strategy2.hs @@ -0,0 +1,35 @@ +module Strategy2 where + +import Datatypes +import Test.QuickCheck (generate, elements) +import VM +import System.Random + +pick :: Int -> [a] -> a +pick n vector = vector !! (n `mod` (length vector)) + +strat2 :: Game -> ([Command],Int) +strat2 game = if new_notes == GameOver + then (new_command : [], score new_game) + else (new_command : (fst $ strat2 new_game), snd $ strat2 new_game) + where + commands = [MoveSE, MoveSW, MoveE, MoveW, RotateClockwise, RotateCounterclockwise] + new_games = map (\cmd -> (step game cmd, cmd)) commands + ok_games = filter (is_ok.snd.fst) new_games + where is_ok (OK) = True + is_ok _ = False + + locked_games = filter (is_locked.snd.fst) new_games + where is_locked (Lock _) = True + is_locked _ = False + over_games = filter (is_over.snd.fst) new_games + where is_over (GameOver) = True + is_over _ = False + + ((new_game,new_notes),new_command) = if (length ok_games) > 0 + then pick 100 ok_games + else if (length locked_games) > 0 + then pick 100 locked_games + else pick 100 over_games + +