53 lines
1.6 KiB
Haskell
Raw Normal View History

module Puzzles.Day1 (puzzle) where
2025-12-02 15:19:11 +00:00
2025-12-08 12:48:49 +00:00
import Pre
puzzle :: Puzzle
puzzle =
2025-12-02 15:19:11 +00:00
Puzzle
{ number = 1
, parser = const $ ((,) <$> ((char 'L' $> L) <|> (char 'R' $> R)) <*> (Inc <$> decimal)) `sepEndBy` newline
2025-12-02 15:19:11 +00:00
, parts =
( sum
. ( flip evalState 50
. traverse \(d, i) -> do
modify $ snd . step i d
p' <- get
pure $ Count if p' == 0 then 1 else 0
)
)
/\ ( sum
. flip evalState 50
. traverse \(d, i) -> do
p <- get
c <- state $ step i d
p' <- get
pure case d of
R -> abs c
L ->
if
| p == 0 -> abs c - 1
| p' == 0 -> abs c + 1
| otherwise -> abs c
)
/\ nil
2025-12-04 21:16:46 +00:00
, extraTests = mempty
2025-12-02 15:19:11 +00:00
}
data Direction = L | R
deriving (Eq, Ord, Show, Generic, NFData)
2025-12-02 15:19:11 +00:00
newtype Pos = Pos Int
deriving newtype (Eq, Ord, Show, Num, NFData)
2025-12-02 15:19:11 +00:00
newtype Inc = Inc Int
deriving newtype (Eq, Ord, Show, Num, NFData)
2025-12-02 15:19:11 +00:00
newtype Count = Count Int
deriving newtype (Eq, Ord, Show, Num, NFData)
2025-12-02 15:19:11 +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