refactoring in playground, use zip file for web
This commit is contained in:
92
playground/samples/aoc2023/Day2.newt
Normal file
92
playground/samples/aoc2023/Day2.newt
Normal file
@@ -0,0 +1,92 @@
|
||||
module Day2
|
||||
|
||||
/-
|
||||
I ported a couple of Advent of Code 2023 solutions from Lean4
|
||||
as an early test case. Here I've adapted them to the web playground
|
||||
by replacing `readFile` with an async `fetchText`.
|
||||
-/
|
||||
|
||||
import Web
|
||||
|
||||
Draw : U
|
||||
Draw = Int × Int × Int
|
||||
|
||||
data Game : U where
|
||||
MkGame : Int -> List Draw -> Game
|
||||
|
||||
-- Original had class and instance...
|
||||
-- Add, Sub, Mul, Neg
|
||||
|
||||
max : Int -> Int -> Int
|
||||
max x y = case x < y of
|
||||
True => y
|
||||
False => x
|
||||
|
||||
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)`
|
||||
|
||||
maxd : Draw -> Draw -> Draw
|
||||
maxd (a,b,c) (d,e,f) = (max a d, max b e, max c f)
|
||||
|
||||
lte : Draw -> Draw -> Bool
|
||||
lte (a,b,c) (d,e,f) = a <= d && b <= e && c <= f
|
||||
|
||||
parseColor : String -> Either String Draw
|
||||
parseColor line = case split line " " of
|
||||
(n :: "red" :: Nil) => Right (toInt n,0,0)
|
||||
(n :: "green" :: Nil) => Right (0,toInt n,0)
|
||||
(n :: "blue" :: Nil) => Right (0,0,toInt n)
|
||||
x => Left $ "Bad draw" ++ repr x
|
||||
|
||||
parseDraw : String -> Either String Draw
|
||||
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 =
|
||||
-- Need the Idris | sugar...
|
||||
case split line ": " of
|
||||
-- this is splitting on the Nil instead of the a
|
||||
(a :: b :: Nil) => case split a " " of
|
||||
("Game" :: ns :: Nil) =>
|
||||
let num = toInt ns in
|
||||
case mapM {Either String} parseDraw $ split b "; " of
|
||||
Right parts => Right $ MkGame num parts
|
||||
Left err => Left err
|
||||
_ => Left "No Game"
|
||||
_ => Left $ "No colon in " ++ line
|
||||
|
||||
part1 : List Game -> Int
|
||||
part1 Nil = 0
|
||||
part1 (MkGame n parts :: rest) =
|
||||
let total = foldl maxd (0,0,0) parts in
|
||||
case lte total (12,13,14) of
|
||||
True => n + part1 rest
|
||||
False => part1 rest
|
||||
|
||||
part2 : List Game -> Int
|
||||
part2 Nil = 0
|
||||
part2 (MkGame n parts :: rest) =
|
||||
case foldl maxd (0,0,0) parts of
|
||||
(a,b,c) => a * b * c + part2 rest
|
||||
|
||||
-- readFile not in browser / playground
|
||||
|
||||
run : String -> Async Unit
|
||||
run fn = do
|
||||
text <- fetchText fn
|
||||
case mapM {Either String} parseGame (split (trim text) "\n") of
|
||||
Left err => putStrLn $ "fail " ++ err
|
||||
Right games => do
|
||||
putStrLn "part1"
|
||||
printLn (part1 games)
|
||||
putStrLn "part2"
|
||||
printLn (part2 games)
|
||||
|
||||
main : IO Unit
|
||||
main = runAsync (do
|
||||
run "aoc2023/day2/eg.txt"
|
||||
run "aoc2023/day2/input.txt"
|
||||
)
|
||||
Reference in New Issue
Block a user