garnet/app/Main.hs

115 lines
3.4 KiB
Haskell
Raw Normal View History

2025-12-02 01:37:59 +00:00
module Main (main) where
2025-12-02 10:54:12 +00:00
import Control.Monad
2025-12-02 01:37:59 +00:00
import Control.Monad.State
2025-12-02 01:44:53 +00:00
import Data.Bifunctor
2025-12-02 08:23:53 +00:00
import Data.ByteString.Lazy qualified as BL
import Data.Functor
2025-12-02 10:54:12 +00:00
import Data.Maybe
2025-12-02 08:23:53 +00:00
import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.Encoding (encodeUtf8)
2025-12-02 09:11:42 +00:00
import Data.Text.IO qualified as T
2025-12-02 09:08:43 +00:00
import Data.Void
2025-12-02 08:23:53 +00:00
import Test.Tasty
import Test.Tasty.Golden (goldenVsString)
2025-12-02 09:30:39 +00:00
import Test.Tasty.Ingredients.ConsoleReporter
2025-12-02 09:08:43 +00:00
import Text.Megaparsec hiding (Pos)
import Text.Megaparsec.Char
import Text.Megaparsec.Char.Lexer qualified as Lex
2025-12-02 00:32:49 +00:00
main :: IO ()
2025-12-02 08:23:53 +00:00
main =
2025-12-02 09:38:31 +00:00
defaultMain
. localOption (Always :: UseColor)
$ testGroup
2025-12-02 08:23:53 +00:00
"tests"
[ puzzleTest puzzle1
2025-12-02 10:54:12 +00:00
, puzzleTest puzzle2
2025-12-02 08:23:53 +00:00
]
2025-12-02 02:01:37 +00:00
2025-12-02 08:23:53 +00:00
puzzleTest :: Puzzle a -> TestTree
puzzleTest p =
2025-12-02 08:53:18 +00:00
testGroup pt $
["examples", "real"] <&> \t ->
2025-12-02 08:55:32 +00:00
withResource (parseFile $ "inputs/" <> t <> "/" <> pt) mempty \input ->
2025-12-02 08:51:26 +00:00
testGroup t $
zip (map show [1 :: Int ..]) p.parts <&> \(n, pp) ->
2025-12-02 08:53:18 +00:00
goldenVsString n ("outputs/" <> t <> "/" <> pt <> "/" <> n) $
BL.fromStrict . encodeUtf8 . pp <$> input
2025-12-02 08:53:18 +00:00
where
pt = show p.number
2025-12-02 10:46:06 +00:00
parseFile fp =
either (fail . ("parse failure: " <>) . errorBundlePretty) pure . runParser (p.parser <* eof) fp
=<< T.readFile fp
2025-12-02 02:01:37 +00:00
data Puzzle input = Puzzle
{ number :: Word
2025-12-02 09:11:42 +00:00
, parser :: Parsec Void Text input
, parts :: [input -> Text]
2025-12-02 02:01:37 +00:00
}
puzzle1 :: Puzzle [(Direction, Inc)]
puzzle1 =
Puzzle
{ number = 1
2025-12-02 09:08:43 +00:00
, parser = flip sepEndBy newline $ (,) <$> ((char 'L' $> L) <|> (char 'R' $> R)) <*> (Inc <$> Lex.decimal)
, parts =
2025-12-02 11:15:49 +00:00
[ T.show
. sum
. flip evalState 50
. traverse \(d, i) -> state \p ->
let (_, p') = step i d p
in (Count if p' == 0 then 1 else 0, p')
, T.show
. sum
. flip evalState 50
. traverse \(d, i) -> state \p ->
let (c, p') = step i d p
c' = case d of
R -> abs c
L ->
if
| p == 0 -> abs c - 1
| p' == 0 -> abs c + 1
| otherwise -> abs c
in (c', p')
]
2025-12-02 02:01:37 +00:00
}
2025-12-02 01:37:59 +00:00
data Direction = L | R
deriving (Eq, Ord, Show)
newtype Pos = Pos Int
deriving newtype (Eq, Ord, Show, Num)
newtype Inc = Inc Int
deriving newtype (Eq, Ord, Show, Num)
newtype Count = Count Int
deriving newtype (Eq, Ord, Show, Num)
2025-12-02 01:44:53 +00:00
step :: Inc -> Direction -> Pos -> (Count, Pos)
step (Inc i) d (Pos p) = bimap Count Pos case d of
L -> (p - i) `divMod` 100
R -> (p + i) `divMod` 100
2025-12-02 10:54:12 +00:00
puzzle2 :: Puzzle [(ID, ID)]
puzzle2 =
Puzzle
{ number = 2
, parser = (<* newline) $ flip sepBy (char ',') $ (,) <$> (Lex.decimal <* char '-') <*> Lex.decimal
, parts =
2025-12-02 11:15:49 +00:00
[ T.show
. sum
. concatMap
(mapMaybe (\n -> guard (isRepetition n) $> n) . uncurry enumFromTo)
]
2025-12-02 10:54:12 +00:00
}
newtype ID = ID Int
deriving newtype (Eq, Ord, Show, Num, Enum)
isRepetition :: ID -> Bool
isRepetition (T.show -> n) = uncurry (==) $ T.splitAt (T.length n `div` 2) n