Skip to content

Instantly share code, notes, and snippets.

@peryaudo
Created December 8, 2013 13:05
Show Gist options
  • Save peryaudo/7857156 to your computer and use it in GitHub Desktop.
Save peryaudo/7857156 to your computer and use it in GitHub Desktop.
import Control.Monad
charToInteger c = toInteger (fromEnum c - fromEnum '0')
cutDown x
| x >= fromIntegral 0 = floor x
| otherwise = ceiling x
-- cards scores
solve :: String -> [Integer] -> [Integer]
solve [] players = players
solve (card:cards) (player:players)
| '0' <= card && card <= '9' = solve cards (players ++ [(player + charToInteger card)])
| card == 'X' =
if length dropped == 0 then
player:players
else
solve (tail dropped) (players ++ [(player * charToInteger (head dropped))])
| card == 'D' =
if length dropped == 0 then
player:players
else
solve (tail dropped) (players ++ [cutDown ((fromIntegral player) / (fromIntegral (charToInteger (head dropped))))])
| card == 'S' =
if length dropped == 0 then
player:players
else
solve (tail dropped) (players ++ [(player - charToInteger (head dropped))])
where
dropped = dropWhile (\c -> not ('0' <= c && c <= '9')) cards
main = do
t <- readLn
replicateM_ t (do
n <- readLn
cards <- getLine
print (maximum (solve cards (replicate n 0))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment