Skip to content

Instantly share code, notes, and snippets.

@Garciat
Last active December 5, 2025 13:11
Show Gist options
  • Select an option

  • Save Garciat/1a187fac35145bb47496af96902f25d7 to your computer and use it in GitHub Desktop.

Select an option

Save Garciat/1a187fac35145bb47496af96902f25d7 to your computer and use it in GitHub Desktop.
data Dial = Dial Int deriving (Show, Eq)
dialMod :: Num a => a
dialMod = 100
instance Num Dial where
(Dial x) + (Dial y) = Dial ((dialMod + x + y) `mod` dialMod)
negate (Dial x) = Dial ((dialMod - x) `mod` dialMod)
fromInteger x = Dial (fromInteger (x `mod` dialMod))
-- other Num methods skipped for brevity
parseDial :: String -> Dial
parseDial cs =
case cs of
'L' : xs -> negate $ val xs
'R' : xs -> val xs
where
val xs = Dial (read xs)
data PasswordState = PasswordState
{ getDial :: Dial
, getPassword :: Int
}
deriving (Show, Eq)
processDials :: [Dial] -> Int
processDials = getPassword . foldl' handleDial initialState
where
initialState = PasswordState (Dial 50) 0
handleDial ps d = updatePassword (applyDial d ps)
applyDial d ps = ps { getDial = getDial ps + d }
updatePassword (PasswordState 0 p) = PasswordState 0 (p + 1)
updatePassword ps = ps
main :: IO ()
main = interact $ (++"\n") . show . processDials . map parseDial . lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment