Add c simulator

This commit is contained in:
Stefan Bühler 2010-06-21 16:20:37 +02:00
parent f3f90b7106
commit 6a103bc8ab

View File

@ -1,4 +1,3 @@
import Data.Maybe import Data.Maybe
import Text.ParserCombinators.ReadP import Text.ParserCombinators.ReadP
import Data.List import Data.List
@ -121,7 +120,8 @@ key_circuit_str = "19L:12R13R0#1R12R,14R0L0#4R9L,9R10R0#3L8L,2L17R0#5L9R,15R1L0#
-- goal: Find a circuit with test_key_circ circ == True -- goal: Find a circuit with test_key_circ circ == True
test_key_circ :: Circuit -> Bool test_key_circ :: Circuit -> Bool
test_key_circ circ = (key == execcirc circ) -- test_key_circ circ = (key == execcirc circ)
test_key_circ circ = checkcirc circ input key
-- key: 11021210112101221 -- key: 11021210112101221
factory0 = parseCircuit "0L:X0R0#X0R:0L" factory0 = parseCircuit "0L:X0R0#X0R:0L"
@ -154,10 +154,10 @@ build s = let (p, pins) = step (-1) [] (reverse (key ++ (readstream s))) in Circ
1 -> step (k+2) (gates ++ [k+5,k+3,k+4,k+1,p,k]) xs 1 -> step (k+2) (gates ++ [k+5,k+3,k+4,k+1,p,k]) xs
2 -> step (k+2) (gates ++ [k+5,k+3,k+1,k+4,k,p]) xs 2 -> step (k+2) (gates ++ [k+5,k+3,k+1,k+4,k,p]) xs
data Circuit = Circuit { outPin :: Int, inPins :: [Int] } deriving (Eq) data Circuit = Circuit { outPin :: Int, inPins :: [Int] } deriving (Eq, Show)
instance Show Circuit where -- instance Show Circuit where
show = showCircuit -- show = showCircuit
circfactory :: Circuit -> ([Nat], ([Nat], Nat) -> ([Nat], Nat)) circfactory :: Circuit -> ([Nat], ([Nat], Nat) -> ([Nat], Nat))
circfactory circ = (map (const 0) (inPins circ), next) where circfactory circ = (map (const 0) (inPins circ), next) where
@ -233,11 +233,18 @@ execfactory :: (a, (a, Nat) -> (a, Nat)) -> [Nat] -> [Nat]
execfactory (s, f) [] = [] execfactory (s, f) [] = []
execfactory (s, f) (x:xs) = o:execfactory (t, f) xs where (t, o) = f (s, x) execfactory (s, f) (x:xs) = o:execfactory (t, f) xs where (t, o) = f (s, x)
execcirc circ = execfactory (circfactory circ) input checkcirc :: Circuit -> [Nat] -> [Nat] -> Bool
checkcirc c input output = let inp = take (length output) (input ++ repeat 0) in output == execCirc c inp
-- checkcirc c input output = findcirc (circ_to_native c) input output
execCirc c input = execfactory (circfactory c) input
-- execCirc = eec
execcirc c = execCirc c input
ec circ = execfactory (circfactory circ) (input ++ take 100 (repeat 0)) ec circ = execfactory (circfactory circ) (input ++ take 100 (repeat 0))
-- ec c = eec c (input ++ take 100 (repeat 0))
readstream :: String -> [Int] readstream :: String -> [Int]
readstream = map (\c -> read [c] :: Int) readstream = map (\c -> read [c] :: Int)
key_input = [0,2,2,2,2,2,2,0,2,1,0,1,1,0,0,1,1] key_input = [0,2,2,2,2,2,2,0,2,1,0,1,1,0,0,1,1]
key = execfactory (circfactory key_circuit) key_input key = execCirc key_circuit key_input