import Data.List.Split import qualified Data.List as L import Data.Vector as V import Data.Char import System.Exit main = do content <- readFile "input" let input = fromList $ L.concatMap (L.map read . splitOn ",") (lines content) exec_step (input,0) opl x | n`L.elem`"1278"=4 | n`L.elem`"56"=3 | n`L.elem`"34"=2 | otherwise=1 where n = L.last $ show x parsemodes :: String -> [Bool] parsemodes m = L.replicate (3 - L.length l) False L.++ l where l = L.map (toEnum . digitToInt) m paramch m opvec t = imap f (V.tail opvec) where f i a = if (L.reverse m) !! i then a else t ! a getopmodes opvec = (op_dedup,parsed_modes) where (op,modes) = L.splitAt 2 $ L.reverse $ show $ opvec ! 0 parsed_modes = parsemodes $ L.reverse modes op_dedup = if L.last op == '0' then [L.head op] else op step opvec (t,p) = do let (op,m) = getopmodes opvec input <- if op=="3" then do line <- getLine return (read line) else return(0) let params = paramch m opvec t newtm <- case op of "1" -> return ((t // [((V.last opvec), (params ! 0) + (params ! 1))]),p) "2" -> return ((t // [((V.last opvec), (params ! 0) * (params ! 1))]),p) "3" -> return ((t // [((V.last opvec), input)]),p) "4" -> do a <- print (V.last $ params) return (t,p) "5" -> return (t, if (params ! 0)/=0 then (params ! 1) else p) "6" -> return (t, if (params ! 0)==0 then (params ! 1) else p) "7" -> return ((t // [((V.last opvec), if (params ! 0)<(params ! 1) then 1 else 0)]),p) "8" -> return ((t // [((V.last opvec), if (params ! 0)==(params ! 1) then 1 else 0)]),p) "99" -> exitSuccess return newtm exec_step (t,p) = do let command_length = opl $ t ! p let opvec = slice p command_length t newtm <- step opvec (t,p+command_length) exec_step newtm