Refactor day 1 to make more use of state monad

This commit is contained in:
George Thomas 2025-12-03 17:52:23 +00:00
parent b9e886c4e4
commit a81d622125

View File

@ -18,22 +18,24 @@ puzzle =
[ T.show [ T.show
. sum . sum
. flip evalState 50 . flip evalState 50
. traverse \(d, i) -> state \p -> . traverse \(d, i) -> do
let (_, p') = step i d p modify $ snd . step i d
in (Count if p' == 0 then 1 else 0, p') p' <- get
pure $ Count if p' == 0 then 1 else 0
, T.show , T.show
. sum . sum
. flip evalState 50 . flip evalState 50
. traverse \(d, i) -> state \p -> . traverse \(d, i) -> do
let (c, p') = step i d p p <- get
c' = case d of c <- state $ step i d
p' <- get
pure case d of
R -> abs c R -> abs c
L -> L ->
if if
| p == 0 -> abs c - 1 | p == 0 -> abs c - 1
| p' == 0 -> abs c + 1 | p' == 0 -> abs c + 1
| otherwise -> abs c | otherwise -> abs c
in (c', p')
] ]
} }