Skip to content

Instantly share code, notes, and snippets.

@petitviolet
Last active October 3, 2015 12:26
Show Gist options
  • Save petitviolet/f2e0e982915dfa20b2b8 to your computer and use it in GitHub Desktop.
Save petitviolet/f2e0e982915dfa20b2b8 to your computer and use it in GitHub Desktop.
Paiza_C024.hs
import Control.Monad
import Data.List.Split
type Pair = (Int, Int)
-- |
-- >>> set (0, 0) 1 2
-- (2,0)
set :: Pair -> Int -> Int -> Pair
set (n1, n2) 1 x = (x, n2)
set (n1, n2) 2 x = (n1, x)
-- |
-- >>> add (0, 0) 5
-- (0,5)
-- >>> add (10, 3) 2
-- (10,12)
add :: Pair -> Int -> Pair
add (n1, n2) n = (n1, n1 + n)
-- |
-- >>> sub (0, 0) 3
-- (0,-3)
-- >>> sub (10, 3) 2
-- (10,8)
sub :: Pair -> Int -> Pair
sub (n1, n2) n = (n1, n1 - n)
parse :: Pair -> [String] -> Pair
parse (n1, n2) ("SET":i:a:[]) = set (n1, n2) (read i :: Int) (read a :: Int)
parse (n1, n2) ("ADD":a:[]) = add (n1, n2) (read a :: Int)
parse (n1, n2) ("SUB":a:[]) = sub (n1, n2) (read a :: Int)
answer :: Pair -> [String] -> Pair
answer (n1, n2) [] = (n1, n2)
answer (n1, n2) (x:xs) = answer (parse (n1, n2) $ splitOn " " x) xs
putPair :: Pair -> String
putPair (n1, n2) = show n1 ++ " " ++ show n2
-- |
-- >>> c024 (0, 0) ["SET 1 10", "SET 2 20", "ADD 40"]
-- "10 50"
-- >>> c024 (0, 0) ["SET 1 -23", "SUB 77", "SET 1 0"]
-- "0 -100"
c024 :: Pair -> [String] -> String
c024 (n1, n2) inputs = putPair $ answer (0, 0) inputs
main = do
n <- readLn
inputs <- replicateM n getLine
print $ c024 (0, 0) inputs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment