diff --git a/2019/day7/day7.hs b/2019/day7/day7.hs index 047487e..a94bf5a 100644 --- a/2019/day7/day7.hs +++ b/2019/day7/day7.hs @@ -2,8 +2,10 @@ import Data.List.Split import qualified Data.List as L import Data.Vector as V import Data.Char +import Control.DeepSeq as DeepSeq data OutAction = Continue | Output | Halt deriving (Enum, Eq, Show) +data Mode = Position | Immediate | Relative deriving (Enum, Eq, Show) type Tape = Vector Int type TapeSection = Vector Int type TuringMachine = (Tape,Int) @@ -52,22 +54,20 @@ getOpModes opvec = (op_dedup,parsed_modes) op_dedup = if L.last op == '0' then [L.head op] else op step :: TapeSection -> (TuringMachine, [Int], [Int]) -> (TuringMachine, OutAction, [Int], [Int]) -step opvec ((t,p),input,val) = case op of - "1" -> (tm_binop (+),Continue,input,val) - "2" -> (tm_binop (*),Continue,input,val) - "3" -> (new_tm t (L.head input),Continue,L.tail input,val) - "4" -> ((t,p),Output,input,V.last params:val) - "5" -> ((t, if params ! 0/=0 then params ! 1 else p),Continue,input,val) - "6" -> ((t, if params ! 0==0 then params ! 1 else p),Continue,input,val) - "7" -> (new_tm t (if (params ! 0)<(params ! 1) - then 1 else 0),Continue,input,val) - "8" -> (new_tm t (if (params ! 0)==(params ! 1) - then 1 else 0),Continue,input,val) - "99" -> ((t,p),Halt,input,val) +step opvec ((t,p),input,output) = case op of + "1" -> (tm_binop (+),Continue,input,output) + "2" -> (tm_binop (*),Continue,input,output) + "3" -> (new_tm t $ L.head input,Continue,L.tail input,output) + "4" -> ((t,p),Output,input,V.last params:output) + "5" -> ((t, if params ! 0/=0 then params ! 1 else p),Continue,input,output) + "6" -> ((t, if params ! 0==0 then params ! 1 else p),Continue,input,output) + "7" -> (tm_binop (\x y->if x (tm_binop (\x y->if x==y then 1 else 0),Continue,input,output) + "99" -> ((t,p),Halt,input,output) where (op,m) = getOpModes opvec params = paramChange m opvec t tm_binop x = new_tm t ((params ! 0) `x` (params ! 1)) - new_tm t x = (t // [(V.last opvec, x)],p) + new_tm t x = DeepSeq.force (t // [(V.last opvec, x)],p) execSteps :: (TuringMachine, [Int], [Int], OutAction) -> (TuringMachine, [Int], [Int], OutAction) execSteps ((t,p),input,output,halt) =