Compare example results against expectations
This commit is contained in:
parent
ac194d1d61
commit
c0e76ae1c5
21
app/Main.hs
21
app/Main.hs
@ -2,17 +2,25 @@ module Main (main) where
|
|||||||
|
|
||||||
import Control.Monad.State
|
import Control.Monad.State
|
||||||
import Data.Bifunctor
|
import Data.Bifunctor
|
||||||
|
import Data.Foldable
|
||||||
import Text.Read (readMaybe)
|
import Text.Read (readMaybe)
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
runPuzzle puzzle1
|
runTests puzzle1
|
||||||
|
|
||||||
runPuzzle :: Puzzle a -> IO ()
|
runTests :: Puzzle a -> IO ()
|
||||||
runPuzzle p = do
|
runTests p = do
|
||||||
Just input <- p.parse <$> readFile ("inputs/examples/" <> show p.number)
|
Just input <- p.parse <$> readFile ("inputs/examples/" <> show p.number)
|
||||||
putStrLn $ p.part1.solve input
|
for_ [(1 :: Word, p.part1), (2, p.part2)] \(n, pp) ->
|
||||||
putStrLn $ p.part2.solve input
|
let r = pp.solve input
|
||||||
|
in putStrLn $
|
||||||
|
show n
|
||||||
|
<> ": "
|
||||||
|
<> if r == pp.expected
|
||||||
|
then "correct!"
|
||||||
|
else "got " <> r <> ", expected " <> pp.expected
|
||||||
|
|
||||||
data Puzzle input = Puzzle
|
data Puzzle input = Puzzle
|
||||||
{ number :: Word
|
{ number :: Word
|
||||||
, parse :: String -> Maybe input
|
, parse :: String -> Maybe input
|
||||||
@ -21,6 +29,7 @@ data Puzzle input = Puzzle
|
|||||||
}
|
}
|
||||||
data Part input = Part
|
data Part input = Part
|
||||||
{ solve :: input -> String
|
{ solve :: input -> String
|
||||||
|
, expected :: String
|
||||||
}
|
}
|
||||||
|
|
||||||
puzzle1 :: Puzzle [(Direction, Inc)]
|
puzzle1 :: Puzzle [(Direction, Inc)]
|
||||||
@ -44,6 +53,7 @@ puzzle1 =
|
|||||||
. traverse \(d, i) -> state \p ->
|
. traverse \(d, i) -> state \p ->
|
||||||
let (_, p') = step i d p
|
let (_, p') = step i d p
|
||||||
in (Count if p' == 0 then 1 else 0, p')
|
in (Count if p' == 0 then 1 else 0, p')
|
||||||
|
, expected = "3"
|
||||||
}
|
}
|
||||||
, part2 =
|
, part2 =
|
||||||
Part
|
Part
|
||||||
@ -61,6 +71,7 @@ puzzle1 =
|
|||||||
| p' == 0 -> abs c + 1
|
| p' == 0 -> abs c + 1
|
||||||
| otherwise -> abs c
|
| otherwise -> abs c
|
||||||
in (c', p')
|
in (c', p')
|
||||||
|
, expected = "6"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user