port aoc2023 day4

more stuff in Prelude, typechecking fixes, solving autos
This commit is contained in:
2024-11-30 10:27:06 -08:00
parent baeaf4295d
commit d5a4d6253f
10 changed files with 234 additions and 41 deletions

View File

@@ -21,14 +21,6 @@ pfunc repr : {a : U} -> a -> String := `(a,o) => ''+o`
pfunc jrepr : {a : U} -> a -> String := `(a,o) => JSON.stringify(o, null, ' ')`
pfunc toInt : String -> Int := `s => Number(s)`
mapM : a b c. (a -> Either b c) -> List a -> Either b (List c)
mapM f Nil = Right Nil
mapM f (x :: xs) = case f x of
Left msg => Left msg
Right v => case mapM f xs of
Left msg => Left msg
Right vs => Right $ v :: vs
maxd : Draw -> Draw -> Draw
maxd (a,b,c) (d,e,f) = (max a d, max b e, max c f)
@@ -42,10 +34,12 @@ parseColor line = case split line " " of
(n :: "blue" :: Nil) => Right (0,0,toInt n)
x => Left $ "Bad draw" ++ repr x
-- FIXME implicit isn't being solved in time here.
parseDraw : String -> Either String Draw
parseDraw line = case mapM parseColor $ split line ", " of
Right parts => Right $ foldl maxd (0,0,0) parts
Left err => Left err
parseDraw line =
case mapM {Either String} parseColor $ split line ", " of
Right parts => Right $ foldl maxd (0,0,0) parts
Left err => Left err
parseGame : String -> Either String Game
parseGame line =
@@ -55,7 +49,7 @@ parseGame line =
(a :: b :: Nil) => case split a " " of
("Game" :: ns :: Nil) =>
let num = toInt ns in
case mapM parseDraw $ split b "; " of
case mapM {Either String} parseDraw $ split b "; " of
Right parts => Right $ MkGame num parts
Left err => Left err
_ => Left "No Game"
@@ -79,7 +73,7 @@ run : String -> IO Unit
run fn = do
putStrLn fn
text <- readFile fn
case mapM parseGame (split (trim text) "\n") of
case mapM {Either String} parseGame (split (trim text) "\n") of
Left err => putStrLn $ "fail " ++ err
Right games => do
putStrLn "part1"