日本情報オリンピック 第5回 予選 第3問目

冗長に。

module Main (main) where

import System
import Control.Monad

data Dice = Dice { v1 :: (Int, Int), v2 :: (Int,Int), v3 :: (Int,Int) } deriving (Show)

north :: Dice -> Dice
north d = Dice { v1 = v2 d, v2 = swap (v1 d), v3 = v3 d }

east :: Dice -> Dice
east d = Dice { v1 = swap (v3 d), v2 = v2 d, v3 = v1 d }

west :: Dice -> Dice
west d = Dice { v1 = v3 d, v2 = v2 d, v3 = swap (v1 d) }

south :: Dice -> Dice
south d = Dice { v1 = swap (v2 d), v2 = v1 d, v3 = v3 d }

left :: Dice -> Dice
left d = Dice { v1 = v1 d, v2 = swap (v3 d), v3 = v2 d }

right :: Dice -> Dice
right d = Dice { v1 = v1 d, v2 = v3 d, v3 = swap (v2 d) }

top :: Dice -> Int
top = fst . v1

swap (x,y) = (y,x)

roll :: String -> Dice -> Dice
roll "North" d = north d
roll "East" d  = east d
roll "West" d  = west d
roll "South" d = south d
roll "Left" d  = left d
roll "Right" d = right d
roll s d       = error $ "Invalid direction : " ++ s


main :: IO ()
main = do
    (inputFile:_) <- getArgs
    ls <- liftM lines $ readFile inputFile
    let
        directions = take (read (head ls)) $ tail ls
        ret = sum $ map top $ scanl (flip roll) dice directions
    writeFile "output.txt" $ show ret
  where
    dice = Dice (1,6) (2,5) (3,4)