Day 2 Part 1

This commit is contained in:
Nathan McCarty 2022-12-02 21:43:53 -05:00
parent 3afb01ca78
commit ce30fd06f4
Signed by: thatonelutenist
GPG Key ID: D70DA3DD4D1E9F96
2 changed files with 2568 additions and 0 deletions

68
02/Main.idr Normal file
View File

@ -0,0 +1,68 @@
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
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
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
main: IO ()
main =
do file <- readFile "input"
case file of
Right content =>
let rounds = catMaybes . map parseRound . lines $ content
in do putStr "Part 1: "
printLn $ foldl (+) 0 (map scoreRound rounds)
Left err => printLn err

2500
02/input Normal file

File diff suppressed because it is too large Load Diff