115 lines
4.0 KiB
Haskell
Raw Normal View History

2025-12-04 10:00:15 +00:00
module Puzzles.Day4 (puzzle) where
2025-12-08 12:48:49 +00:00
import Pre
2025-12-04 12:23:48 +00:00
import Data.Sequence qualified as Seq
2025-12-05 12:09:49 +00:00
import Data.Stream.Infinite qualified as S
import Data.Text.Lazy qualified as TL
2025-12-08 12:48:49 +00:00
import Data.Text.Lazy.Encoding qualified as TL
2025-12-05 12:09:49 +00:00
import Data.Text.Lazy.IO qualified as TL
2025-12-04 10:00:15 +00:00
puzzle :: Puzzle
puzzle =
Puzzle
{ number = 4
2025-12-04 12:23:48 +00:00
, parser = flip sepEndBy newline $ some $ asum $ enumerate <&> \t -> char (inToChar t) $> t
2025-12-04 10:00:15 +00:00
, parts =
[ TL.show
2025-12-05 12:09:49 +00:00
. (\g -> countRolls g - countRolls (removeAccessibleRolls $ findAccessible g))
. mkGrid
, TL.show
2025-12-05 12:09:49 +00:00
. (\g -> countRolls g - countRolls (fst $ S.head $ S.filter (noneAccessible . snd) $ generateFrames g))
. mkGrid
]
, extraTests = \isRealData path input ->
[ testCase "round trip" do
t <- TL.readFile if isRealData then "../inputs/real/4" else "../inputs/examples/4"
input' <- input
t @=? drawGrid (mkGrid input' <&> \case InEmpty -> OutEmpty; InRoll -> OutRoll)
, testGroup
"frames"
let frames = Seq.fromList . takeUntil noneAccessible . fmap snd . generateFrames . mkGrid <$> input
nFrames = if isRealData then 58 else 9
in ( [0 .. nFrames] <&> \n ->
goldenVsString (show n) (path <> "frames/" <> show n) $
2025-12-08 12:48:49 +00:00
TL.encodeUtf8 . maybe "frame list too short!" drawGrid . Seq.lookup n <$> frames
2025-12-05 12:09:49 +00:00
)
<> [ testCase "end" do
2025-12-05 12:09:49 +00:00
Just g <- Seq.lookup nFrames <$> frames
assertBool "accessible tile found" $ noneAccessible g
]
2025-12-04 10:00:15 +00:00
]
}
2025-12-04 12:23:48 +00:00
2025-12-05 12:09:49 +00:00
newtype Grid a = Grid (Seq (Seq (V2 Int, a)))
deriving (Functor)
2025-12-04 20:00:53 +00:00
2025-12-04 12:23:48 +00:00
data InTile
= InEmpty
| InRoll
deriving (Eq, Ord, Show, Enum, Bounded)
inToChar :: InTile -> Char
inToChar = \case
InEmpty -> '.'
InRoll -> '@'
data OutTile
= OutEmpty
| OutRoll
| OutAccessible
deriving (Eq, Ord, Show, Enum, Bounded)
outToChar :: OutTile -> Char
outToChar = \case
2025-12-05 12:09:49 +00:00
OutEmpty -> inToChar InEmpty
OutRoll -> inToChar InRoll
2025-12-04 12:23:48 +00:00
OutAccessible -> 'x'
2025-12-05 12:09:49 +00:00
drawGrid :: Grid OutTile -> TL.Text
drawGrid (Grid g) = TL.unlines . toList . fmap (TL.pack . toList . fmap outToChar) $ snd <<$>> g
mkGrid :: [[a]] -> Grid a
mkGrid = Grid . Seq.fromList . map Seq.fromList . zipWith (map . first . V2) [0 ..] . map (zip [0 ..])
findAccessible :: Grid InTile -> Grid OutTile
findAccessible (Grid inGrid) =
Grid $
inGrid <<&>> \(v, t) -> (v,) case t of
InEmpty -> OutEmpty
InRoll ->
if length (filter ((== Just InRoll) . fmap snd) neighbours) < 4
then OutAccessible
else OutRoll
where
neighbours = do
x <- [-1 .. 1]
y <- [-1 .. 1]
guard $ not (x == 0 && y == 0)
let V2 x' y' = v + V2 x y
pure $ Seq.lookup x' inGrid >>= Seq.lookup y'
removeAccessibleRolls :: Grid OutTile -> Grid InTile
removeAccessibleRolls = fmap \case
2025-12-04 20:00:53 +00:00
OutEmpty -> InEmpty
OutRoll -> InRoll
OutAccessible -> InEmpty
2025-12-05 12:09:49 +00:00
generateFrames :: Grid InTile -> Stream (Grid InTile, Grid OutTile)
generateFrames = unfoldMutual findAccessible removeAccessibleRolls
noneAccessible :: Grid OutTile -> Bool
noneAccessible (Grid g) = not $ any (elem OutAccessible . fmap snd) g
countRolls :: Grid InTile -> Int
countRolls (Grid g) = length $ concatMap (filter (== InRoll) . toList . fmap snd) g
2025-12-04 12:23:48 +00:00
(<<$>>) :: (Functor f1, Functor f2) => (a -> b) -> f1 (f2 a) -> f1 (f2 b)
(<<$>>) = fmap . fmap
(<<&>>) :: (Functor f1, Functor f2) => f1 (f2 a) -> (a -> b) -> f1 (f2 b)
(<<&>>) = flip (<<$>>)
2025-12-05 12:09:49 +00:00
takeUntil :: (Foldable t) => (a -> Bool) -> t a -> [a]
takeUntil p = foldr (\x xs -> x : if p x then [] else xs) []
unfoldMutual :: (a -> b) -> (b -> a) -> a -> Stream (a, b)
unfoldMutual f g a = let b = f a in (a, b) :> unfoldMutual f g (g b)