Skip to content

Instantly share code, notes, and snippets.

module Sensible
( foo
, bar
, Baz (..)
, Silly.okName
)
import Very.Silly.Named.Module as Silly
foo = Silly.longFooName
import qualified Foo.Bar as FB
import Baz
( foo
, fooBar
, fooBarBaz
)
foo = map . bar baz
where
bar = ...
baz = ...
runInstruction :: Intcode -> Prog Intcode
runInstruction ic = do
opcodes <- currentOpCode ic
case opcodes of
(One, m1, m2, m3) -> do
newIc <- op1 m1 m2 m3 (ip ic) (code ic)
return $ moveIp 4 . updateCode newIc $ ic
...
(!!!) :: Integral a => MonadFail m => [a] -> Int -> m a
infixl 9 !!!
xs !!! i
| i < 0 = fail $ "index " ++ show i ++ " less that zero"
| i >= length xs = return 0
| otherwise = return $ xs !! i
(!!!) :: Integral a => [a] -> Int -> Maybe a
infixl 9 !!!
xs !!! i
| i < 0 = Nothing
| i >= length xs = Just 0
| otherwise = Just $ xs !! i
instance Functor Prog where
fmap _ (Crashed e) = Crashed e
fmap f (End a) = End (f a)
fmap f (Running a) = Running (f a)
fmap f (AwaitInput a) = AwaitInput (f a)
instance Applicative Prog where
pure = Running
_ <*> (Crashed e) = Crashed e
(End f) <*> prog = fmap f prog
instance Monad Prog where
return = Running
(Crashed e) >>= _ = (Crashed e)
(End prog) >>= k = k prog
(Running prog) >>= k = k prog
(AwaitInput prog) >>= k = k prog
instance MonadFail Prog where
fail = Crashed
data Prog a = Running a | AwaitInput a | End a | Crashed String deriving(Show, Eq)
data Intcode = Intcode {
input::[Int],
code::[Int],
-- ip: Instruction Pointer
ip::Int,
-- rb: Relative Base
rb::Int,
output::[Int]