merge aoc lib and prelude

This commit is contained in:
2024-11-23 15:08:49 -08:00
parent dda0bf6fb9
commit 5cbe594993
14 changed files with 472 additions and 103 deletions

View File

@@ -1,6 +1,6 @@
module Day1
import Lib
import Prelude
digits1 : List Char -> List Int
digits1 Nil = Nil
@@ -48,27 +48,22 @@ part1 text digits =
let nums = map combine $ map digits lines in
foldl _+_ 0 nums
-- Hack from before I had support for typeclasses
infixl 1 _>>_
_>>_ : {A B : U} -> A -> B -> B
a >> b = b
#check digits1 unpack : String -> List Int
runFile : String -> Dummy
runFile fn =
let text = readFile fn in
log fn >>
log "part1" >>
log (part1 text (digits1 unpack)) >>
log "part2" >>
log (part1 text (digits2 unpack)) >>
log ""
runFile : String -> IO Unit
runFile fn = do
text <- readFile fn
putStrLn fn
putStrLn "part1"
putStrLn $ show (part1 text (digits1 unpack))
putStrLn "part2"
putStrLn $ show (part1 text (digits2 unpack))
putStrLn ""
-- Argument is a hack to keep it from running at startup. Need to add IO
main : Int -> Dummy
main _ =
runFile "aoc2023/day1/eg.txt" >>
runFile "aoc2023/day1/eg2.txt" >>
main : IO Unit
main = do
runFile "aoc2023/day1/eg.txt"
runFile "aoc2023/day1/eg2.txt"
runFile "aoc2023/day1/input.txt"

View File

@@ -1,9 +1,9 @@
module Day2
import Lib
import Prelude
Draw : U
Draw = Pair Int (Pair Int Int)
Draw = Int × Int × Int
data Game : U where
MkGame : Int -> List Draw -> Game
@@ -68,11 +68,6 @@ parseGame line =
_ => Left "No Game"
_ => Left $ "No colon in " ++ line
infixl 1 _>>_
_>>_ : {A B : U} -> A -> B -> B
a >> b = b
part1 : List Game -> Int
part1 Nil = 0
part1 (MkGame n parts :: rest) =
@@ -87,18 +82,18 @@ part2 (MkGame n parts :: rest) =
case foldl maxd (0,0,0) parts of
(a,b,c) => a * b * c + part2 rest
run : String -> Dummy
run fn =
let text = readFile fn in
run : String -> IO Unit
run fn = do
text <- readFile fn
case mapM parseGame (split (trim text) "\n") of
Left err => log $ "fail " ++ err
Right games =>
log "part1" >>
log (part1 games) >>
log "part2" >>
log (part2 games)
Left err => putStrLn $ "fail " ++ err
Right games => do
putStrLn "part1"
printLn (part1 games)
putStrLn "part2"
printLn (part2 games)
main : Dummy -> Dummy
main _ =
run "aoc2023/day2/eg.txt" >>
main : IO Unit
main = do
run "aoc2023/day2/eg.txt"
run "aoc2023/day2/input.txt"