-passesSecurity :: Machine -> [String] -> [String] -> Bool
-passesSecurity machine instructions drops = not ("Alert" `isInfixOf` output)
- where output = attemptSecurity machine instructions drops
+passesSecurity :: [String] -> CachedMachine Bool
+passesSecurity drops =
+ do output <- attemptSecurity drops
+ return $ not ("Alert" `isInfixOf` output)
+
+attemptSecurity :: [String] -> CachedMachine String
+attemptSecurity drops =
+ do let dropCommands = map ("drop " ++ ) drops
+ output <- runCachedMachine dropCommands
+ return output
+
+runCachedMachine :: [String] -> CachedMachine String
+runCachedMachine dropCommands =
+ do (machine, instructions) <- ask
+ let (_, _, output) = runMachine (encodeCommands (instructions ++ dropCommands ++ ["north"])) machine
+ return $ decodeOutput output
+