Skip to content

Instantly share code, notes, and snippets.

@thelissimus
Last active December 3, 2025 17:06
Show Gist options
  • Select an option

  • Save thelissimus/d5a88014ea569eda373f920c141a116e to your computer and use it in GitHub Desktop.

Select an option

Save thelissimus/d5a88014ea569eda373f920c141a116e to your computer and use it in GitHub Desktop.
module Main (main) where
import Control.Monad.State
main :: IO ()
main = print . solve =<< readFile "input.txt"
data Dir = L Int | R Int
parseDir :: String -> Dir
parseDir = \case
'L' : xs -> L (read xs)
'R' : xs -> R (read xs)
_ -> undefined
applyDir :: Dir -> State Int Int
applyDir dir = do
curr <- get
put case dir of
L n -> (curr - n) `mod` 100
R n -> (curr + n) `mod` 100
get
solve :: String -> Int
solve = length . filter (0 ==) . flip evalState 50 . traverse (applyDir . parseDir) . lines
module Main (main) where
import Control.Monad.State
main :: IO ()
main = print . solve =<< readFile "input.txt"
data Dir = L Int | R Int
parseDir :: String -> Dir
parseDir = \case
'L' : xs -> L (read xs)
'R' : xs -> R (read xs)
_ -> undefined
clicks :: Int -> Dir -> Int
clicks curr = \case
L n -> (n `div` 100) + if curr > 0 && curr <= (n `mod` 100) then 1 else 0
R n -> (n `div` 100) + if curr > 0 && (100 - curr) <= (n `mod` 100) then 1 else 0
applyDir :: Dir -> State Int Int
applyDir dir = do
curr <- get
put case dir of
L n -> (curr - n) `mod` 100
R n -> (curr + n) `mod` 100
pure $ clicks curr dir
solve :: String -> Int
solve = sum . flip evalState 50 . traverse (applyDir . parseDir) . lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment