diff --git a/solutions/Days/Day02.hs b/solutions/Days/Day02.hs index 75724dc..2d9a6fc 100644 --- a/solutions/Days/Day02.hs +++ b/solutions/Days/Day02.hs @@ -15,38 +15,26 @@ data Shape = Rock | Paper | Scissors data Outcome = Win | Lose | Draw deriving (Eq) -convertToShape :: T.Text -> Shape -convertToShape x - | x == "A" || x == "X" = Rock - | x == "B" || x == "Y" = Paper - | x == "C" || x == "Z" = Scissors - | otherwise = error $ show x +toShape :: T.Text -> Shape +toShape c + | c == "A" || c == "X" = Rock + | c == "B" || c == "Y" = Paper + | c == "C" || c == "Z" = Scissors + | otherwise = error $ show c -convertToOutcome :: T.Text -> Outcome -convertToOutcome "X" = Lose -convertToOutcome "Y" = Draw -convertToOutcome "Z" = Win +toOutcome :: T.Text -> Outcome +toOutcome "X" = Lose +toOutcome "Y" = Draw +toOutcome "Z" = Win +toOutcome c = error $ show c -parseInput :: T.Text -> [(Shape, T.Text)] -parseInput input = map someFunc . T.lines $ input -- ["A Y", "B X"] -> [(Rock, Paper), (Paper, Scissors)] - where - someFunc :: T.Text -> (Shape, T.Text) - someFunc line = (convertToShape first, second) - where - [first, second] = T.words $ line - -score :: Shape -> Int -score Rock = 1 -score Paper = 2 -score Scissors = 3 - -part1 :: [(Shape, T.Text)] -> Int -part1 input = sum $ map (\(opp, player) -> outcomeScore (opp, player) + score player) input' - where - input' = map (B.second convertToShape) input +pScore :: Shape -> Int +pScore Rock = 1 +pScore Paper = 2 +pScore Scissors = 3 -outcomeScore :: (Shape, Shape) -> Int -outcomeScore (opp, player) +oScore :: (Shape, Shape) -> Int +oScore (opp, player) | player == beats opp = 6 | player == loses opp = 0 | otherwise = 3 @@ -61,15 +49,21 @@ loses Rock = Scissors loses Paper = Rock loses Scissors = Paper +scores :: [(Shape, Shape)] -> [Int] +scores = map (\(o, p) -> oScore (o, p) + pScore p) + +parseInput :: T.Text -> [(Shape, T.Text)] +parseInput = map convert . T.lines + where + convert line = let [fst, snd] = T.words line in (toShape fst, snd) + +part1 :: [(Shape, T.Text)] -> Int +part1 input = sum $ scores $ map (B.second toShape) input + part2 :: [(Shape, T.Text)] -> Int -part2 input = sum $ map (\(opp, player) -> outcomeScore (opp, player) + score player) shapeList +part2 input = sum $ scores $ map tupleToShape outcomes where - outcomeList = map (B.second convertToOutcome) input -- [(Shape, Outcome)] - shapeList = map outcomeToShape outcomeList - outcomeToShape (shape, outcome) = - let outcome' = - case outcome of - Win -> beats shape - Lose -> loses shape - Draw -> shape - in (shape, outcome') + outcomes = map (B.second toOutcome) input + tupleToShape (shape, Win) = (shape, beats shape) + tupleToShape (shape, Lose) = (shape, loses shape) + tupleToShape (shape, _) = (shape, shape)