Last active
December 3, 2025 17:06
-
-
Save thelissimus/d5a88014ea569eda373f920c141a116e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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