2022-12-02 21:43:53 -05:00
|
|
|
import Data.String
|
|
|
|
import System.File.ReadWrite
|
|
|
|
|
|
|
|
data Result = Win | Loss | Draw
|
|
|
|
|
|
|
|
-- Score value of a result for us
|
|
|
|
scoreResult : Result -> Int
|
|
|
|
scoreResult Win = 6
|
|
|
|
scoreResult Loss = 0
|
|
|
|
scoreResult Draw = 3
|
|
|
|
|
2022-12-02 21:56:24 -05:00
|
|
|
-- Parse a result from a string
|
|
|
|
parseResult : String -> Maybe Result
|
|
|
|
parseResult "X" = Just Loss
|
|
|
|
parseResult "Y" = Just Draw
|
|
|
|
parseResult "Z" = Just Win
|
|
|
|
parseResult x = Nothing
|
|
|
|
|
2022-12-02 21:43:53 -05:00
|
|
|
data Move = Rock | Paper | Scissors
|
|
|
|
|
|
|
|
-- Determine the Result for a pair of moves
|
|
|
|
-- First argument is their move, second argument is our move
|
|
|
|
eval : Move -> Move -> Result
|
|
|
|
eval Rock Rock = Draw
|
|
|
|
eval Rock Paper = Win
|
|
|
|
eval Rock Scissors = Loss
|
|
|
|
eval Paper Rock = Loss
|
|
|
|
eval Paper Paper = Draw
|
|
|
|
eval Paper Scissors = Win
|
|
|
|
eval Scissors Rock = Win
|
|
|
|
eval Scissors Paper = Loss
|
|
|
|
eval Scissors Scissors = Draw
|
|
|
|
|
|
|
|
-- Score for an individual move
|
|
|
|
scoreMove : Move -> Int
|
|
|
|
scoreMove Rock = 1
|
|
|
|
scoreMove Paper = 2
|
|
|
|
scoreMove Scissors = 3
|
|
|
|
|
|
|
|
-- Parse a move from a string
|
|
|
|
parseMove : String -> Maybe Move
|
|
|
|
parseMove "A" = Just Rock
|
|
|
|
parseMove "B" = Just Paper
|
|
|
|
parseMove "C" = Just Scissors
|
|
|
|
parseMove "X" = Just Rock
|
|
|
|
parseMove "Y" = Just Paper
|
|
|
|
parseMove "Z" = Just Scissors
|
|
|
|
parseMove x = Nothing
|
|
|
|
|
2022-12-02 21:56:24 -05:00
|
|
|
-- Get the move that gives the desired result
|
|
|
|
getResult : Move -> Result -> Move
|
|
|
|
getResult Rock Win = Paper
|
|
|
|
getResult Rock Loss = Scissors
|
|
|
|
getResult Rock Draw = Rock
|
|
|
|
getResult Paper Win = Scissors
|
|
|
|
getResult Paper Loss = Rock
|
|
|
|
getResult Paper Draw = Paper
|
|
|
|
getResult Scissors Win = Rock
|
|
|
|
getResult Scissors Loss = Paper
|
|
|
|
getResult Scissors Draw = Scissors
|
|
|
|
|
2022-12-02 21:43:53 -05:00
|
|
|
data Round = R Move Move
|
|
|
|
|
|
|
|
-- Score a round
|
|
|
|
scoreRound : Round -> Int
|
|
|
|
scoreRound (R a b) = scoreMove b + scoreResult (eval a b)
|
|
|
|
|
|
|
|
-- Parse a round from a string
|
|
|
|
parseRound : String -> Maybe Round
|
|
|
|
parseRound input =
|
|
|
|
let inputWords = words input
|
|
|
|
in case inputWords of
|
|
|
|
[a, b] =>
|
|
|
|
do theirs <- parseMove a
|
|
|
|
ours <- parseMove b
|
|
|
|
Just (R theirs ours)
|
|
|
|
x => Nothing
|
|
|
|
|
2022-12-02 21:56:24 -05:00
|
|
|
data Strategy = S Move Result
|
|
|
|
|
|
|
|
-- Create the round that results from this strategy
|
|
|
|
getRound : Strategy -> Round
|
|
|
|
getRound (S a b) = R a (getResult a b)
|
|
|
|
|
|
|
|
-- Parse a strategy from a string
|
|
|
|
parseStrategy : String -> Maybe Strategy
|
|
|
|
parseStrategy input =
|
|
|
|
let inputWords = words input
|
|
|
|
in case inputWords of
|
|
|
|
[a, b] =>
|
|
|
|
do theirs <- parseMove a
|
|
|
|
ours <- parseResult b
|
|
|
|
Just (S theirs ours)
|
|
|
|
x => Nothing
|
|
|
|
|
2022-12-02 21:43:53 -05:00
|
|
|
main: IO ()
|
|
|
|
main =
|
|
|
|
do file <- readFile "input"
|
|
|
|
case file of
|
|
|
|
Right content =>
|
|
|
|
let rounds = catMaybes . map parseRound . lines $ content
|
2022-12-02 21:56:24 -05:00
|
|
|
strats = catMaybes . map parseStrategy . lines $ content
|
2022-12-02 21:43:53 -05:00
|
|
|
in do putStr "Part 1: "
|
|
|
|
printLn $ foldl (+) 0 (map scoreRound rounds)
|
2022-12-02 21:56:24 -05:00
|
|
|
putStr "Part 2: "
|
|
|
|
printLn $ foldl (+) 0 (map scoreRound . map getRound $ strats)
|
2022-12-02 21:43:53 -05:00
|
|
|
Left err => printLn err
|