diff options
Diffstat (limited to '2019/IntCode.hs')
-rw-r--r-- | 2019/IntCode.hs | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/2019/IntCode.hs b/2019/IntCode.hs index 89b54a2..9e94066 100644 --- a/2019/IntCode.hs +++ b/2019/IntCode.hs @@ -1,7 +1,7 @@ {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FlexibleContexts #-} module IntCode ( - IC(..), - parse, decode, run + Arg(..), IC(..), + parse, decode, run, unparse ) where import Control.Monad.ST @@ -31,6 +31,25 @@ parse = map read . splitOn ',' (pre, _ : post) -> pre : splitOn c post _ -> [s] +unparse :: IC -> [Int] +unparse ic = case ic of + Add a b c -> go 1 [a,b,c] + Mul a b c -> go 2 [a,b,c] + Inp a -> go 3 [a ] + Out a -> go 4 [a ] + Jnz a b -> go 5 [a,b ] + Jez a b -> go 6 [a,b ] + Clt a b c -> go 7 [a,b,c] + Ceq a b c -> go 8 [a,b,c] + Hlt -> [99] + where + go code as = (100 * mode as + code) : map bare as + where bare (Imm n) = n + bare (Addr n) = n + mode [] = 0 + mode (Imm _ : as) = 1 + 10 * mode as + mode (Addr _ : as) = 10 * mode as + decode :: [Int] -> (IC, Int) decode [] = error "IC: Execution fell off end of program" decode (ins : rest) = |