feat: complete day 2

main
sgoudham 2 years ago
parent ea30be2bf3
commit d56c54f107
Signed by: hammy
GPG Key ID: 44E818FD5457EEA4

@ -1,16 +1,75 @@
{-# LANGUAGE OverloadedStrings #-}
module Days.Day02 (day02) where module Days.Day02 (day02) where
import AOC (Solution (..)) import AOC (Solution (..))
import qualified Data.Text as T import Data.Bifunctor qualified as B
import Data.Text qualified as T
day02 :: Solution day02 :: Solution
day02 = Solution parseInput part1 part2 day02 = Solution parseInput part1 part2
parseInput :: T.Text -> a data Shape = Rock | Paper | Scissors
parseInput = error "parseInput not defined for day 02" deriving (Eq)
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
convertToOutcome :: T.Text -> Outcome
convertToOutcome "X" = Lose
convertToOutcome "Y" = Draw
convertToOutcome "Z" = Win
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
outcomeScore :: (Shape, Shape) -> Int
outcomeScore (opp, player)
| player == beats opp = 6
| player == loses opp = 0
| otherwise = 3
beats :: Shape -> Shape
beats Rock = Paper
beats Paper = Scissors
beats Scissors = Rock
part1 :: a -> Int loses :: Shape -> Shape
part1 = error "part1 not defined for day 02" loses Rock = Scissors
loses Paper = Rock
loses Scissors = Paper
part2 :: a -> Int part2 :: [(Shape, T.Text)] -> Int
part2 = error "part2 not defined for day 02" part2 input = sum $ map (\(opp, player) -> outcomeScore (opp, player) + score player) shapeList
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')

Loading…
Cancel
Save