Created
December 9, 2022 12:12
-
-
Save justinwyer/12083bc78ed9b0d92e7cc5c454bbabee to your computer and use it in GitHub Desktop.
advent of code 2022 day 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module A200 where | |
import Data.List.Split (splitOn) | |
data Symbol = Rock | Paper | Scissors | |
data Strategy = Win | Lose | Draw | |
data Match = Match { opponent :: Symbol, player :: Symbol } | |
strategy :: Symbol -> Strategy -> Match | |
strategy o s = Match o (case s of | |
Win -> case o of | |
Rock -> Paper | |
Paper -> Scissors | |
Scissors -> Rock | |
Lose -> case o of | |
Rock -> Scissors | |
Paper -> Rock | |
Scissors -> Paper | |
Draw -> o) | |
score :: Match -> Int | |
score (Match Rock Rock) = 4 | |
score (Match Rock Paper) = 8 | |
score (Match Rock Scissors) = 3 | |
score (Match Paper Rock) = 1 | |
score (Match Paper Paper) = 5 | |
score (Match Paper Scissors) = 9 | |
score (Match Scissors Rock) = 7 | |
score (Match Scissors Paper) = 2 | |
score (Match Scissors Scissors) = 6 | |
instance Show Symbol where | |
show Rock = "Rock" | |
show Paper = "Paper" | |
show Scissors = "Scissors" | |
instance Show Match where | |
show (Match o p) = show o ++ " vs " ++ show p | |
instance Read Symbol where | |
readsPrec _ "A" = [(Rock, "")] | |
readsPrec _ "X" = [(Rock, "")] | |
readsPrec _ "B" = [(Paper, "")] | |
readsPrec _ "Y" = [(Paper, "")] | |
readsPrec _ "C" = [(Scissors, "")] | |
readsPrec _ "Z" = [(Scissors, "")] | |
readsPrec _ _ = [] | |
instance Read Strategy where | |
readsPrec _ "X" = [(Lose, "")] | |
readsPrec _ "Y" = [(Draw, "")] | |
readsPrec _ "Z" = [(Win, "")] | |
readsPrec _ _ = [] | |
input = "/Users/justin/Documents/advent/advent101/input201" | |
parseMatch :: String -> Match | |
parseMatch (o:_:p:_) = Match (read [o]) (read [p]) | |
parseStrategy :: String -> Match | |
parseStrategy (o:_:s:_) = strategy (read [o]) (read [s]) | |
a201 :: IO () | |
a201 = readFile input >>= print . sum . map (score. parseMatch) . lines | |
a202 :: IO () | |
a202 = readFile input >>= print . sum . map (score . parseStrategy) . lines |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment