seperate haskell und purescript directories
This commit is contained in:
30
haskell/vindinium/src/BotRunner.hs
Normal file
30
haskell/vindinium/src/BotRunner.hs
Normal file
@@ -0,0 +1,30 @@
|
||||
module BotRunner
|
||||
( Bot
|
||||
, escapedInputInErrorPrefix
|
||||
, runBot
|
||||
) where
|
||||
|
||||
import Control.Monad
|
||||
import System.IO
|
||||
|
||||
-- A Codingame bot where input and output have been abstracted out.
|
||||
type Bot = IO String -> (String -> IO ()) -> IO ()
|
||||
|
||||
escapedInputInErrorPrefix :: String
|
||||
escapedInputInErrorPrefix = "#"
|
||||
|
||||
-- Run a bot in the Codingame Arena (or IDE).
|
||||
runBot
|
||||
:: Bool -- Shall the bot’s input be echoed on stderr to be able to replay any game result?
|
||||
-> Bot -- The bot.
|
||||
-> IO ()
|
||||
runBot echoInput bot = do
|
||||
hSetBuffering stdout NoBuffering
|
||||
bot readLine writeLine
|
||||
where
|
||||
readLine = do
|
||||
line <- getLine
|
||||
when echoInput $
|
||||
hPutStrLn stderr (escapedInputInErrorPrefix ++ line)
|
||||
return line
|
||||
writeLine = putStrLn
|
||||
36
haskell/vindinium/src/Codingame.hs
Normal file
36
haskell/vindinium/src/Codingame.hs
Normal file
@@ -0,0 +1,36 @@
|
||||
module Codingame
|
||||
( bundle
|
||||
) where
|
||||
|
||||
import Codingame.WebServices
|
||||
import Codingame.SourcePackager
|
||||
import Language.Haskell.Exts
|
||||
|
||||
import BotRunner
|
||||
import Player
|
||||
import Debug
|
||||
|
||||
|
||||
sourcePath = "src/Player.hs"
|
||||
|
||||
parseMode :: ParseMode
|
||||
parseMode = ParseMode {
|
||||
parseFilename = "<unknown>.hs",
|
||||
baseLanguage = Haskell2010,
|
||||
extensions = [EnableExtension ScopedTypeVariables, EnableExtension LambdaCase, EnableExtension MultiWayIf],
|
||||
ignoreLanguagePragmas = False,
|
||||
ignoreLinePragmas = True,
|
||||
fixities = Just preludeFixities,
|
||||
ignoreFunctionArity = False
|
||||
}
|
||||
|
||||
bundle :: IO ()
|
||||
bundle = do
|
||||
source <- createMonolithicSourceWithMode parseMode sourcePath
|
||||
credentials <- readCredentials "credentials.json"
|
||||
|
||||
putStrLn source
|
||||
|
||||
let file = "Bundled.hs"
|
||||
writeFile file $ "{-# LANGUAGE ScopedTypeVariables, LambdaCase, MultiWayIf #-}\n" ++ source
|
||||
|
||||
21
haskell/vindinium/src/Debug.hs
Normal file
21
haskell/vindinium/src/Debug.hs
Normal file
@@ -0,0 +1,21 @@
|
||||
module Debug
|
||||
( trace
|
||||
, _trace
|
||||
, traceList
|
||||
,_traceList
|
||||
) where
|
||||
|
||||
import Data.List
|
||||
import qualified Debug.Trace as Trace
|
||||
|
||||
trace :: Show a => String -> a -> a
|
||||
trace message x = Trace.trace (message ++ " = " ++ show x) x
|
||||
|
||||
_trace :: String -> a -> a
|
||||
_trace _ = id
|
||||
|
||||
traceList :: Show a => String -> [a] -> [a]
|
||||
traceList message xs = Trace.trace (message ++ " = [\n\t" ++ intercalate "\n\t" (map show xs) ++ "\n]") xs
|
||||
|
||||
_traceList :: Show a => String -> [a] -> [a]
|
||||
_traceList _ = id
|
||||
99
haskell/vindinium/src/Graph.hs
Normal file
99
haskell/vindinium/src/Graph.hs
Normal file
@@ -0,0 +1,99 @@
|
||||
{-# LANGUAGE ScopedTypeVariables #-}
|
||||
module Graph where
|
||||
|
||||
import Prelude
|
||||
import qualified Data.Map as M
|
||||
import qualified Data.Set as S
|
||||
import Data.Maybe
|
||||
import qualified Data.Sequence as Seq
|
||||
|
||||
newtype Graph v = Graph (M.Map v (Seq.Seq v))
|
||||
|
||||
empty :: forall v. Graph v
|
||||
empty = Graph M.empty
|
||||
|
||||
addNode :: forall v. Ord v => Graph v -> v -> Graph v
|
||||
addNode (Graph m) v = Graph $ M.insert v Seq.empty m
|
||||
|
||||
-- adds an Edge from Node "from" to Node "to"
|
||||
-- returns the graph unmodified if "to" does not exist
|
||||
addEdge :: forall v. Ord v => Graph v -> v -> v -> Graph v
|
||||
addEdge g@(Graph m) from to = Graph $ M.update updateVal from m
|
||||
where
|
||||
updateVal :: Seq.Seq v -> Maybe (Seq.Seq v)
|
||||
updateVal nodes
|
||||
| g `contains` to = Just $ to Seq.<| nodes
|
||||
| otherwise = Just nodes
|
||||
|
||||
toMap :: forall v. Graph v -> M.Map v (Seq.Seq v)
|
||||
toMap (Graph m) = m
|
||||
|
||||
adjacentEdges :: forall v. Ord v => Graph v -> v -> Seq.Seq v
|
||||
adjacentEdges (Graph m) nodeId = fromMaybe Seq.empty $ M.lookup nodeId m
|
||||
|
||||
contains :: forall v. Ord v => Graph v -> v -> Bool
|
||||
contains (Graph m) key = case M.lookup key m of
|
||||
Just _ -> True
|
||||
Nothing -> False
|
||||
|
||||
-- shortestPath :: forall v. Ord v => Graph v -> v -> v -> Seq v
|
||||
-- shortestPath g@(Graph m) from to = reverse $ shortestPath' (from, Seq.empty) Seq.empty S.empty
|
||||
-- where
|
||||
-- shortestPath' :: (v, Seq v) -> [(v, Seq v)] -> S.Set v-> Seq v
|
||||
-- shortestPath' from queue visited
|
||||
-- | fst from == to = snd from
|
||||
-- | length newQueue == 0 = Seq.empty
|
||||
-- | otherwise = shortestPath' (head newQueue) newQueue (S.insert (fst from) visited)
|
||||
-- where
|
||||
-- adjacent :: S.Set v
|
||||
-- adjacent = S.fromList $ adjacentEdges g (fst from)
|
||||
-- newQueue :: [(v, Seq v)]
|
||||
-- newQueue = drop 1 queue <> ( map (\x -> (x, fst from : snd from)) (S.toList $ S.difference adjacent visited) )
|
||||
|
||||
shortestPathList :: forall v. Ord v => Graph v -> v -> v -> Seq.Seq v
|
||||
shortestPathList g@(Graph m) from to = Seq.reverse $ shortestPath' (from, Seq.empty) Seq.empty Seq.empty
|
||||
where
|
||||
shortestPath' :: (v, Seq.Seq v) -> Seq.Seq (v, Seq.Seq v) -> Seq.Seq v-> Seq.Seq v
|
||||
shortestPath' from queue visited
|
||||
| fst from == to = snd from
|
||||
| otherwise = case Seq.viewl newQueue of
|
||||
n Seq.:< _ -> shortestPath' n newQueue (fst from Seq.<| visited)
|
||||
Seq.EmptyL -> Seq.empty
|
||||
where
|
||||
adjacent :: Seq.Seq v
|
||||
adjacent = adjacentEdges g (fst from)
|
||||
newQueue :: Seq.Seq (v, Seq.Seq v)
|
||||
newQueue = Seq.drop 1 queue <> (fmap (\x -> (x, fst from Seq.<| snd from)) (adjacent `without` visited) )
|
||||
|
||||
without :: Eq a => Seq.Seq a -> Seq.Seq a -> Seq.Seq a
|
||||
without seq1 seq2 = Seq.filter (\e -> all ((/=) e) seq2) seq1
|
||||
|
||||
with :: Eq a => Seq.Seq a -> Seq.Seq a -> Seq.Seq a
|
||||
with seq1 seq2 = seq1 <> (seq2 `without` seq1)
|
||||
|
||||
-- pathExists :: forall v. Ord v => Graph v -> v -> v -> Bool
|
||||
-- pathExists g@(Graph m) from to = shortestPath' from Seq.empty Seq.empty
|
||||
-- where
|
||||
-- shortestPath' :: v -> Seq v -> Seq v -> Bool
|
||||
-- shortestPath' from queue visited
|
||||
-- | from == to = True
|
||||
-- | otherwise = case head $ newQueue of
|
||||
-- Just n -> shortestPath' n newQueue (from : visited)
|
||||
-- Nothing -> False
|
||||
-- where
|
||||
-- adjacent :: Seq v
|
||||
-- adjacent = adjacentEdges g from
|
||||
-- newQueue :: Seq v
|
||||
-- newQueue = drop 1 queue <> (adjacent \\ visited)
|
||||
|
||||
dfs :: forall v. Ord v => Graph v -> v -> v -> Bool
|
||||
dfs g@(Graph m) from to = dfs' g from to Seq.empty
|
||||
|
||||
dfs' :: forall v. Ord v => Graph v -> v -> v -> Seq.Seq v -> Bool
|
||||
dfs' g@(Graph m) from to visited
|
||||
| from == to = True
|
||||
| otherwise = any ((==) True) $ subcalls (adjacent `without` visited)
|
||||
where
|
||||
subcalls = fmap (\f -> dfs' g f to visited')
|
||||
visited' = with visited adjacent
|
||||
adjacent = adjacentEdges g from
|
||||
137
haskell/vindinium/src/Player.hs
Normal file
137
haskell/vindinium/src/Player.hs
Normal file
@@ -0,0 +1,137 @@
|
||||
{-# LANGUAGE ScopedTypeVariables, LambdaCase, MultiWayIf #-}
|
||||
module Player
|
||||
( runMain
|
||||
) where
|
||||
|
||||
import System.IO
|
||||
import Control.Monad
|
||||
import System.Random
|
||||
import Data.Char (digitToInt)
|
||||
import Data.List (minimumBy)
|
||||
|
||||
import BotRunner
|
||||
import Graph
|
||||
|
||||
data BoardEntity = BSpawnPoint Int | BWall | BTavern | BMine | BEmpty deriving (Show, Eq)
|
||||
type Board = [[BoardEntity]]
|
||||
type IndexedBoard = [(Pos, BoardEntity)]
|
||||
type Pos = (Int, Int)
|
||||
|
||||
-- Id, Pos, life, gold
|
||||
data Entity
|
||||
= Hero Int Pos Int Int
|
||||
| Mine Int Pos
|
||||
|
||||
runMain :: IO ()
|
||||
runMain = runBot True bot
|
||||
|
||||
bot :: Bot
|
||||
bot readLine writeLine = do
|
||||
-- let graph = foldl addEdge' graph' $ concatMap nodeConnections nodes
|
||||
-- hPrint stderr $ shortestPathList graph "[0,0]" "[6,5]"
|
||||
|
||||
input_line <- getLine
|
||||
let size = read input_line :: Int
|
||||
|
||||
board' <- replicateM size getLine
|
||||
let board :: Board = map (\br -> map (\se -> if
|
||||
| se == '.' -> BEmpty
|
||||
| se == '#' -> BWall
|
||||
| se == 'T' -> BTavern
|
||||
| se == 'M' -> BMine
|
||||
| otherwise -> BSpawnPoint $ digitToInt se) br) board'
|
||||
input_line <- getLine
|
||||
let iBoard :: IndexedBoard = concatMap (\(i_r, br) -> map (\(i_c, bc) -> ((i_c, i_r), bc)) br) $ zip [0..9] $ map (zip [0..9]) board
|
||||
|
||||
let myId = read input_line :: Int -- ID of your hero
|
||||
|
||||
-- game loop
|
||||
forever $ do
|
||||
input_line <- getLine
|
||||
let entitycount = read input_line :: Int -- the number of entities
|
||||
|
||||
entities <- replicateM entitycount $ do
|
||||
input_line <- getLine
|
||||
let input = words input_line
|
||||
let entitytype = input!!0 -- HERO or MINE
|
||||
let id = read (input!!1) :: Int -- the ID of a hero or the owner of a mine
|
||||
let x = read (input!!2) :: Int -- the x position of the entity
|
||||
let y = read (input!!3) :: Int -- the y position of the entity
|
||||
let life = read (input!!4) :: Int -- the life of a hero (-1 for mines)
|
||||
let gold = read (input!!5) :: Int -- the gold of a hero (-1 for mines)
|
||||
pure $ if entitytype == "HERO"
|
||||
then Hero id (x,y) life gold
|
||||
else Mine id (x,y)
|
||||
|
||||
let heroes = filter (\e -> case e of
|
||||
Hero _ _ _ _ -> True
|
||||
_ -> False) entities
|
||||
let hero = head $ filter (\e -> case e of
|
||||
Hero id _ _ _ -> id == myId
|
||||
_ -> False) heroes
|
||||
let mines = filter (\e -> case e of
|
||||
Mine oId _ -> oId /= myId
|
||||
_ -> False) entities
|
||||
let minMine = minimumBy (\e1 e2 -> compare (dist (posFromEntity e1) (posFromEntity hero)) (dist (posFromEntity e2) (posFromEntity hero))) mines
|
||||
let minTavernPos = minimumBy (\p1 p2 -> compare (dist p1 (posFromEntity hero)) (dist p2 (posFromEntity hero))) $ map (\(p, be) -> p) $ filter (\(p, be) -> isTavern be) iBoard
|
||||
-- hPrint stderr minMine
|
||||
|
||||
-- WAIT | NORTH | EAST | SOUTH | WEST
|
||||
r <- randomRIO (0,3) :: IO Int
|
||||
let dir = if r == 0
|
||||
then "NORTH"
|
||||
else if r == 1
|
||||
then "EAST"
|
||||
else if r == 2
|
||||
then "SOUTH"
|
||||
else
|
||||
"WEST"
|
||||
|
||||
putStrLn $ case life hero of
|
||||
Just lp -> if lp < 30 then moveToPos minTavernPos else moveToEntity minMine
|
||||
Nothing -> moveToEntity minMine
|
||||
|
||||
moveToEntity :: Entity -> String
|
||||
moveToEntity e = case e of
|
||||
Hero _ p _ _ -> cout p
|
||||
Mine _ p -> cout p
|
||||
where cout (x,y) = "MOVE " <> (show x) <> " " <> (show y)
|
||||
|
||||
moveToPos :: (Int, Int) -> String
|
||||
moveToPos (x, y) = "MOVE " <> (show x) <> " " <> (show y)
|
||||
|
||||
dist :: Pos -> Pos -> Int
|
||||
dist (x1, y1) (x2, y2) = abs (x2 - x1) + abs (y2 - y1)
|
||||
|
||||
life :: Entity -> Maybe Int
|
||||
life (Hero _ _ l _) = Just l
|
||||
life _ = Nothing
|
||||
|
||||
posFromEntity :: Entity -> (Int, Int)
|
||||
posFromEntity (Hero _ p _ _) = p
|
||||
posFromEntity (Mine _ p) = p
|
||||
|
||||
isTavern :: BoardEntity -> Bool
|
||||
isTavern BTavern = True
|
||||
isTavern _ = False
|
||||
|
||||
addEdge' :: Ord v => Graph v -> [v] -> Graph v
|
||||
addEdge' g v = addEdge'' g v
|
||||
where
|
||||
addEdge'' :: Ord v => Graph v -> [v] -> Graph v
|
||||
addEdge'' g [a,b] = addEdge g a b
|
||||
|
||||
graph' = foldl addNode empty sNodes
|
||||
|
||||
sNodes :: [String]
|
||||
sNodes = map (\n -> show n) nodes
|
||||
|
||||
nodes = do
|
||||
x <- [0..9]
|
||||
y <- [0..9]
|
||||
return [x, y]
|
||||
|
||||
nodeConnections :: [Int] -> [[String]]
|
||||
nodeConnections [x, y] = [ [o, show [x-1,y]], [o, show [x+1,y]], [o, show [x,y-1]], [o, show [x,y+1]] ]
|
||||
where o = show [x, y]
|
||||
nodeConnections _ = []
|
||||
Reference in New Issue
Block a user