From f0fc7f696ff53355f17040fa78e71f61eb9d57ab Mon Sep 17 00:00:00 2001 From: Amneesh Singh Date: Fri, 9 Dec 2022 17:26:31 +0530 Subject: [PATCH] day 2: rewrite use Data.Text Signed-off-by: Amneesh Singh --- day2.hs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/day2.hs b/day2.hs index 7dffd24..3327c5a 100644 --- a/day2.hs +++ b/day2.hs @@ -1,8 +1,12 @@ +import Data.Text (Text) +import qualified Data.Text as T (lines, unpack) +import Lib (readFile') + -- we do not use a simple 9 value case cuz yeah main :: IO () main = do - choices <- parse <$> readFile "day2.in" + choices <- parse <$> readFile' "day2.in" putStr "Q1: " print $ q1 choices putStr "Q2: " @@ -17,8 +21,8 @@ instance Ord Choice where compare Scissors Rock = LT compare _ _ = GT -parse :: String -> [(Choice, Choice)] -parse = map (\[x, _, y] -> (val x 'A', val y 'X')) . lines +parse :: Text -> [(Choice, Choice)] +parse = map ((\[x, _, y] -> (val x 'A', val y 'X')) . T.unpack) . T.lines score :: (Choice, Choice) -> Int score (i, j) = 1 + fromEnum j + 3 * fromEnum (compare j i)