wvogel日記

自分用の技術備忘録が多めです.

AOJ try!

AOJなるものがあるらしく。
まずは簡単なものから解いてみることに。
解答の提出方法とかわからないし、出力の形式整えるの邪魔くさいので、ちゃんとした正答かどうかなんて知りません笑


Haskellで、
ttp://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0500
にある問題をやってみた。

main = do n  <- getLine
          getContents >>=
            print.foldl game (0,0)
              .map (toTouple.words).take (read n).lines

toTouple :: [String]-> (Int,Int)
toTouple (a:b:_) =(read a,read b)

game :: (Int,Int)-> (Int,Int) -> (Int,Int)
game (a,b) (ap,bp) | ap  > bp = (a+ap+bp,b)
                   | ap == bp = (a+ap,b+bp)
                   | ap  < bp = (a,b+ap+bp)

main関数もっとすっきりさせたいですね。
なので、関数を整理してみた。
ついでに、出力用に答えを整形するtrim関数も

main = do n  <- getLine
          getContents >>=
            print.trim.gameFlow.take (read n).lines

trim :: Num a => (a,a) -> String
trim (a,b) = show a ++ " " ++ show b

gameFlow :: [String] -> (Int,Int)
gameFlow = foldl gamescore (0,0).map (toTouple.words)

toTouple :: [String]-> (Int,Int)
toTouple (a:b:_) =(read a,read b)