73 lines
1.3 KiB
Haskell
73 lines
1.3 KiB
Haskell
import System.IO
|
|
|
|
data Part = P1 | P2
|
|
|
|
|
|
|
|
|
|
data Shape = Rock | Paper | Scissors deriving (Eq, Show)
|
|
|
|
|
|
shape :: Char -> Shape
|
|
shape 'A' = Rock
|
|
shape 'B' = Paper
|
|
shape 'C' = Scissors
|
|
shape 'X' = Rock
|
|
shape 'Y' = Paper
|
|
shape 'Z' = Scissors
|
|
|
|
|
|
opShp :: String -> Shape
|
|
opShp = shape . head
|
|
|
|
beats = [(Rock, Scissors),(Scissors, Paper),(Paper, Rock)]
|
|
|
|
myShp2 :: Char -> Shape -> Shape
|
|
myShp2 'X' o = snd $ head $ filter ((== o) . fst) beats
|
|
myShp2 'Y' o = o
|
|
myShp2 'Z' o = fst $ head $ filter ((== o) . snd) beats
|
|
|
|
myShp :: Part -> String -> Shape
|
|
myShp P1 g = shape $ last g
|
|
myShp P2 g = myShp2 (last g) (opShp g)
|
|
|
|
|
|
shapeScore :: Shape -> Int
|
|
shapeScore Rock = 1
|
|
shapeScore Paper = 2
|
|
shapeScore Scissors = 3
|
|
|
|
|
|
instance Ord Shape where
|
|
Rock `compare` Scissors = GT
|
|
Scissors `compare` Rock = LT
|
|
s1 `compare` s2 = (shapeScore s1) `compare` (shapeScore s2)
|
|
|
|
|
|
|
|
|
|
winScore :: (Shape, Shape) -> Int
|
|
winScore (me, op)
|
|
| me < op = 0
|
|
| me == op = 3
|
|
| me > op = 6
|
|
|
|
|
|
|
|
|
|
gameScore :: Part -> String -> Int
|
|
gameScore p s = shapeScore me + winScore (me, op)
|
|
where me = myShp p s
|
|
op = opShp s
|
|
|
|
|
|
|
|
|
|
handler :: String -> String
|
|
handler s = (show $ sum $ map (gameScore P1) $ lines s) ++ "\n" ++
|
|
(show $ sum $ map (gameScore P2) $ lines s) ++ "\n"
|
|
|
|
main :: IO ()
|
|
main = do
|
|
interact handler
|