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)
|
2025-12-02 11:22:17 +00:00
|
|
|
. testGroup "tests"
|
2025-12-02 14:35:17 +00:00
|
|
|
$ ["examples", "real"] <&> \t ->
|
|
|
|
|
testGroup t $
|
|
|
|
|
[ puzzle1
|
|
|
|
|
, puzzle2
|
|
|
|
|
]
|
|
|
|
|
<&> \Puzzle{number, parser, parts} ->
|
|
|
|
|
let
|
|
|
|
|
pt = show number
|
|
|
|
|
parseFile fp =
|
|
|
|
|
either (fail . ("parse failure: " <>) . errorBundlePretty) pure
|
|
|
|
|
. runParser (parser <* eof) fp
|
|
|
|
|
=<< T.readFile fp
|
|
|
|
|
in
|
2025-12-02 14:59:15 +00:00
|
|
|
withResource (parseFile $ "../inputs/" <> t <> "/" <> pt) mempty \input ->
|
2025-12-02 14:35:17 +00:00
|
|
|
testGroup pt $
|
|
|
|
|
zip (map show [1 :: Int ..]) parts <&> \(n, pp) ->
|
2025-12-02 14:59:15 +00:00
|
|
|
goldenVsString n ("../outputs/" <> t <> "/" <> pt <> "/" <> n) $
|
2025-12-02 14:35:17 +00:00
|
|
|
BL.fromStrict . encodeUtf8 . pp <$> input
|
2025-12-02 08:07:33 +00:00
|
|
|
|
2025-12-02 11:21:04 +00:00
|
|
|
data Puzzle = forall input. Puzzle
|
2025-12-02 02:01:37 +00:00
|
|
|
{ number :: Word
|
2025-12-02 09:11:42 +00:00
|
|
|
, parser :: Parsec Void Text input
|
2025-12-02 11:14:54 +00:00
|
|
|
, parts :: [input -> Text]
|
2025-12-02 02:01:37 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-02 11:21:04 +00:00
|
|
|
puzzle1 :: Puzzle
|
2025-12-02 02:01:37 +00:00
|
|
|
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)
|
2025-12-02 11:14:54 +00:00
|
|
|
, 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 11:14:54 +00:00
|
|
|
]
|
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
|
|
|
|
2025-12-02 11:21:04 +00:00
|
|
|
puzzle2 :: Puzzle
|
2025-12-02 10:54:12 +00:00
|
|
|
puzzle2 =
|
|
|
|
|
Puzzle
|
|
|
|
|
{ number = 2
|
|
|
|
|
, parser = (<* newline) $ flip sepBy (char ',') $ (,) <$> (Lex.decimal <* char '-') <*> Lex.decimal
|
2025-12-02 11:14:54 +00:00
|
|
|
, parts =
|
2025-12-02 11:15:49 +00:00
|
|
|
[ T.show
|
|
|
|
|
. sum
|
|
|
|
|
. concatMap
|
2025-12-02 12:33:41 +00:00
|
|
|
(mapMaybe (\n -> guard (isRepetition2 n) $> n) . uncurry enumFromTo)
|
|
|
|
|
, T.show
|
|
|
|
|
. sum
|
|
|
|
|
. concatMap
|
|
|
|
|
(mapMaybe (\n -> guard (isRepetitionN n) $> n) . uncurry enumFromTo)
|
2025-12-02 11:14:54 +00:00
|
|
|
]
|
2025-12-02 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newtype ID = ID Int
|
|
|
|
|
deriving newtype (Eq, Ord, Show, Num, Enum)
|
|
|
|
|
|
2025-12-02 12:33:41 +00:00
|
|
|
isRepetition2 :: ID -> Bool
|
2025-12-02 12:53:31 +00:00
|
|
|
isRepetition2 (T.show -> n) = case T.length n `divMod` 2 of
|
|
|
|
|
(d, 0) -> equalChunks n d
|
|
|
|
|
_ -> False
|
2025-12-02 12:33:41 +00:00
|
|
|
|
|
|
|
|
isRepetitionN :: ID -> Bool
|
2025-12-02 12:53:31 +00:00
|
|
|
isRepetitionN (T.show -> n) = flip any [1 .. T.length n `div` 2] $ equalChunks n
|
|
|
|
|
|
|
|
|
|
equalChunks :: Text -> Int -> Bool
|
|
|
|
|
equalChunks n i = case T.chunksOf i n of
|
|
|
|
|
[] -> True
|
2025-12-02 12:33:41 +00:00
|
|
|
x : xs -> all (== x) xs
|