garnet/app/Main.hs

55 lines
1.4 KiB
Haskell
Raw Normal View History

2025-12-02 01:37:59 +00:00
module Main (main) where
import Control.Monad.State
2025-12-02 01:44:53 +00:00
import Data.Bifunctor
2025-12-02 01:37:59 +00:00
import Data.Traversable
import Text.Read (readMaybe)
2025-12-02 00:32:49 +00:00
main :: IO ()
2025-12-02 01:37:59 +00:00
main = do
Just input <-
traverse
( \case
'L' : (readMaybe -> Just i) -> Just (L, Inc i)
'R' : (readMaybe -> Just i) -> Just (R, Inc i)
_ -> Nothing
)
. lines
<$> readFile "inputs/examples/1"
print
. sum
. flip evalState 50
$ for input \(d, i) -> state \p ->
2025-12-02 01:44:53 +00:00
let (_, p') = step i d p
2025-12-02 01:37:59 +00:00
in (Count if p' == 0 then 1 else 0, p')
2025-12-02 01:44:53 +00:00
print
. sum
. flip evalState 50
$ for input \(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 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