Added ternary encoding/decoding
This commit is contained in:
parent
531942cf1d
commit
f70ae4b43b
137
Car.hs
137
Car.hs
@ -2,28 +2,133 @@
|
||||
import Data.Packed
|
||||
import Data.Maybe
|
||||
|
||||
data Car = Car { chambers :: [Chamber] }
|
||||
data Car = Car { airparts :: Int, chambers :: [Chamber] }
|
||||
data Chamber = MainChamber { upperPipe, lowerPipe :: Pipe } | AuxChamber { upperPipe, lowerPipe :: Pipe }
|
||||
data Pipe = Pipe { sections :: [Section] }
|
||||
data Section = Section { tank, inComp, outComp :: Int }
|
||||
data Section = Section { tank :: Int }
|
||||
|
||||
data Fuel = Matrix Int
|
||||
-- 22 11 22 00010 22 100100 22 00110 22 1001011 22 100101110 22 110101011 22 101010010 22 0010
|
||||
-- 22 111001101111010 22 010010111 22 011010 22 1001011
|
||||
-- 22 11100 22 01011 22 001010 22 0100 22 001110 22 0110 22 0121110 22 0120
|
||||
-- 22 10 22 0000 22 01011 22 010100 22 0011 22 011110 22 0010
|
||||
|
||||
-- Cars
|
||||
-- 2211 220 0 0 1 0 2210 0 1 0 0 2200 1 1 0 2210 0 1 0 1 1 2210 0 1 0 1 1 1 0 2211 0 1 0 1 0 1 1 2210 1 0 1 0 0 1 0 220 0 1 0
|
||||
-- 22 111001101111010 22 010010111 22 011010 22 1001011
|
||||
-- 2211 1 0 0 220 1 0 1 1 220 0 1 0 1 0 220 1 0 0 220 0 1 1 1 0 220 1 1 0 220 1 21110 220 1 20
|
||||
-- 2210 220 0 0 0 220 1 0 1 1 220 1 0 1 0 0 220 0 1 1 220 1 1 1 1 0 220 0 1 0
|
||||
|
||||
-- 11100000: for 1 tanks, using 1 ingredients of air, check fuel for tank 0, c_{1,1} must be >= 1
|
||||
-- 220 220 0 0 0 1 1 0 220 1 0 1 0 0 1 0
|
||||
-- [ ([0,0],0,[1]), ([1,1],0,[0]) ]
|
||||
|
||||
-- 1 220 0 0 0 1 0 : 1 chamber, 1 tank
|
||||
-- [([0,0],0,[0])]
|
||||
|
||||
-- 1 220 0 0 0 1 1 0: In the car, tank 1 is not properly connected to tank 0
|
||||
-- [([0,0],0,[1])]
|
||||
|
||||
-- 1 220 0 0 0 1 1 1: In the car, tank 2 is not properly connected to tank 0
|
||||
-- 1 220 0 0 0 1 1 2: In the car, tank 3 is not properly connected to tank 0
|
||||
-- 1 220 0 0 0 1 22000: In the car, tank 4 is not properly connected to tank 0
|
||||
|
||||
-- correct fuels:
|
||||
-- 1 1 1 220: for 1 tanks, using 1 ingredients of air, checking reaction chamber 0
|
||||
-- 220 1 1 1 1 1 1 1: for 2 tanks, using 1 ingredients of air, checking reaction chamber 0, checking reaction chamber 1, surplus of ingredient 1 in lower pipe when air consists of ingredient 1 only
|
||||
-- [[2],[1]]
|
||||
-- 1111: for 1 tanks, using 1 ingredients of air, checking reaction chamber 0, first fuel component must increase
|
||||
|
||||
-- 1110: for 1 tanks, using 1 ingredients of air, check fuel for tank 0, c_{1,1} must be >= 1
|
||||
-- 220111011: for 2 tanks, using 1 ingredients of air, check fuel for tank 1, c_{1,1} must be >= 1
|
||||
-- 220111111: for 2 tanks, using 1 ingredients of air, check fuel for tank 1, c_{1,1} must be >= 1
|
||||
-- 2201 1111: for 2 tanks, using 1 ingredients of air, dimension mismatch
|
||||
-- 10: for 1 tanks, using 0 ingredients of air, dimension mismatch
|
||||
-- 11: for 1 tanks, using 1 ingredients of air, dimension mismatch
|
||||
-- 11011000000: for 1 tanks, using 1 ingredients of air, dimension mismatch
|
||||
-- 111220000: for 1 tanks, using 1 ingredients of air, fuel coefficients missing for tank 1
|
||||
-- 1111220000: for 1 tanks, using 1 ingredients of air, fuel coefficients missing for tank 1
|
||||
|
||||
-- 1220000: for 1 tanks, using 2 ingredients of air, dimension mismatch
|
||||
-- 12210000: for 1 tanks, using 3 ingredients of air, dimension mismatch
|
||||
-- 122220000000000000: for 1 tanks, using 6 ingredients of air, dimension mismatch
|
||||
-- 1220: for 1 tanks, using 2 ingredients of air, dimension mismatch
|
||||
-- 12210: for 1 tanks, using 3 ingredients of air, dimension mismatch
|
||||
-- 12211: for 1 tanks, using 4 ingredients of air, dimension mismatch
|
||||
-- 12222000: for 1 tanks, using 6 ingredients of air, dimension mismatch
|
||||
-- 12222001: for 1 tanks, using 7 ingredients of air, dimension mismatch
|
||||
-- 1222201: for 1 tanks, using 9 ingredients of air, dimension mismatch
|
||||
-- 122221: for 1 tanks, using 15 ingredients of air, dimension mismatch
|
||||
-- 2200: for 2 tanks, using 0 ingredients of air, dimension mismatch
|
||||
-- 2202200: for 2 tanks, using 2 ingredients of air, dimension mismatch
|
||||
-- 220222202200: for 2 tanks, using 14 ingredients of air, dimension mismatch
|
||||
-- 22102200000000: for 3 tanks, using 2 ingredients of air, dimension mismatch
|
||||
-- 2210100220101: for 3 Tanks, using 1 ingredients of air, dimension mismatch
|
||||
-- 221102200000000: for 4 tanks, using 0 ingredients of air, dimension mismatch
|
||||
-- 2201: for 2 tanks, using 1 ingredients of air, dimension mismatch
|
||||
-- 220220: for 2 tanks, using 2 ingredients of air, dimension mismatch
|
||||
-- 2202222022: for 2 tanks, using 14 ingredients of air, dimension mismatch
|
||||
-- 22100: for 3 tanks, using 0 ingredients of air, dimension mismatch
|
||||
-- 22101: for 3 tanks, using 1 ingredients of air, dimension mismatch
|
||||
-- 2210220: for 3 tanks, using 2 ingredients of air, dimension mismatch
|
||||
-- 22102210: for 3 tanks, using 3 ingredients of air, dimension mismatch
|
||||
-- 22110: for 4 tanks, using 0 ingredients of air, dimension mismatch
|
||||
-- 22111: for 4 tanks, using 1 ingredients of air, dimension mismatch
|
||||
-- 22120: for 5 tanks, using 0 ingredients of air, dimension mismatch
|
||||
-- 22121: for 5 tanks, using 1 ingredients of air, dimension mismatch
|
||||
-- 22220001: for 6 tanks, using 1 ingredients of air, dimension mismatch
|
||||
-- 22220011: for 7 tanks, using 1 ingredients of air, dimension mismatch
|
||||
-- 22220101: for 9 tanks, using 1 ingredients of air, dimension mismatch
|
||||
-- 22220111: for 10 tanks, using 1 ingredients of air, dimension mismatch
|
||||
-- 222210001: for 16 tanks, using 0 ingredients of air, dimension mismatch
|
||||
-- 2222100011: for 16 tanks, using 1 ingredients of air, dimension mismatch
|
||||
-- 222210010: for 18 tanks, using 0 ingredients of air, dimension mismatch
|
||||
-- 2222100101: for 18 tanks, using 1 ingredients of air, dimension mismatch
|
||||
-- 222210011: for 19 tanks, using 0 ingredients of air, dimension mismatch
|
||||
-- 2222100111: for 19 tanks, using 1 ingredients of air, dimension mismatch
|
||||
|
||||
|
||||
-- Numbers
|
||||
-- 0: 0
|
||||
-- 1: 10
|
||||
-- 2: 11
|
||||
-- 3: 12
|
||||
-- 4: 22000
|
||||
-- 5: 22001
|
||||
-- 6: 22002
|
||||
-- 7: 22010
|
||||
-- 8: 22011
|
||||
-- 9: 22012
|
||||
-- 10: 22020
|
||||
-- 11: 22021
|
||||
-- 12: 22022
|
||||
|
||||
-- List Lengths:
|
||||
-- 0: 0
|
||||
-- 1: 1
|
||||
-- 2: 220
|
||||
-- 3: 2210
|
||||
-- 4: 2211
|
||||
-- 5: 2212
|
||||
-- 6: 2222000
|
||||
-- 7: 2222001
|
||||
-- 8: 2222002
|
||||
-- 9: 2222010
|
||||
-- 10: 2222011
|
||||
-- 11: 2222012
|
||||
-- 12: 2222020
|
||||
-- 13: 2222021
|
||||
-- 14: 2222022
|
||||
-- 15: 222210000
|
||||
-- 16: 222210001
|
||||
-- 17: 222210002
|
||||
-- 18: 222210010
|
||||
-- 19: 222210011
|
||||
-- 20: 222210012
|
||||
-- 21: 222210020
|
||||
-- 22: 222210021
|
||||
-- 23: 222210022
|
||||
-- 24: 222210100
|
||||
-- 25: 222210101
|
||||
-- 26: 222210102
|
||||
-- 27: 222210110
|
||||
-- 28: 222210111
|
||||
-- 29: 222210112
|
||||
-- 30: 222210120
|
||||
-- 31: 222210121
|
||||
-- 32: 222210122
|
||||
-- 33: 222210200
|
||||
-- 34: 222210201
|
||||
-- 35: 222210202
|
||||
-- 36: 222210210
|
||||
-- 37: 222210211
|
||||
-- 38: 222210212
|
||||
-- 39: 222210220
|
||||
-- 40: 222210221
|
||||
-- 41: 222210222
|
||||
|
70
Circuit.hs
70
Circuit.hs
@ -11,6 +11,66 @@ type Nat = Int
|
||||
-- example:
|
||||
-- build ""
|
||||
|
||||
type Dec a = ([Nat] -> (a, [Nat]))
|
||||
type Enc a = a -> String
|
||||
|
||||
decodeNumber :: Dec Integer
|
||||
decodeNumber (0:xs) = (0,xs)
|
||||
decodeNumber (1:0:xs) = (1,xs)
|
||||
decodeNumber (1:1:xs) = (2,xs)
|
||||
decodeNumber (1:2:xs) = (3,xs)
|
||||
decodeNumber (2:2:0:0:0:xs) = (4,xs)
|
||||
decodeNumber (2:2:0:0:1:xs) = (5,xs)
|
||||
decodeNumber (2:2:0:0:2:xs) = (6,xs)
|
||||
decodeNumber (2:2:0:1:0:xs) = (7,xs)
|
||||
decodeNumber (2:2:0:1:1:xs) = (8,xs)
|
||||
decodeNumber (2:2:0:1:2:xs) = (9,xs)
|
||||
decodeNumber (2:2:0:2:0:xs) = (10,xs)
|
||||
decodeNumber (2:2:0:2:1:xs) = (11,xs)
|
||||
decodeNumber (2:2:0:2:2:xs) = (12,xs)
|
||||
|
||||
|
||||
decodeListLen :: Dec Int
|
||||
decodeListLen (0:xs) = (0,xs)
|
||||
decodeListLen (1:xs) = (1,xs)
|
||||
decodeListLen (2:2:0:xs) = (2,xs)
|
||||
decodeListLen (2:2:1:0:xs) = (3,xs)
|
||||
decodeListLen (2:2:1:1:xs) = (4,xs)
|
||||
decodeListLen (2:2:1:2:xs) = (5,xs)
|
||||
decodeListLen (2:2:2:2:0:0:0:xs) = (6,xs)
|
||||
decodeListLen (2:2:2:2:0:0:1:xs) = (7,xs)
|
||||
decodeListLen (2:2:2:2:0:0:2:xs) = (8,xs)
|
||||
decodeListLen (2:2:2:2:0:1:0:xs) = (9,xs)
|
||||
decodeListLen (2:2:2:2:0:1:1:xs) = (10,xs)
|
||||
decodeListLen (2:2:2:2:0:1:2:xs) = (11,xs)
|
||||
decodeListLen (2:2:2:2:0:2:0:xs) = (12,xs)
|
||||
decodeListLen (2:2:2:2:0:2:1:xs) = (13,xs)
|
||||
decodeListLen (2:2:2:2:0:2:2:xs) = (14,xs)
|
||||
|
||||
decodeTuple2 :: (Dec a, Dec b) -> Dec (a, b)
|
||||
decodeTuple2 (f, g) x = let (a, x1) = f x in let (b, x2) = g x1 in ((a,b), x2)
|
||||
decodeTuple3 :: (Dec a, Dec b, Dec c) -> Dec (a, b, c)
|
||||
decodeTuple3 (f, g, h) x = let (a, x1) = f x in let (b, x2) = g x1 in let (c, x3) = h x2 in ((a,b,c), x3)
|
||||
|
||||
decodeList :: Dec a -> Dec [a]
|
||||
decodeList f x = let (len,x1) = decodeListLen x in get [] len x1 where
|
||||
get r 0 x = (r, x)
|
||||
get r k x = let (e,xn) = f x in get (r ++ [e]) (k-1) xn
|
||||
|
||||
decodeNumberList = decodeList decodeNumber
|
||||
|
||||
decodeCar = fst . decodeList (decodeTuple3 (decodeNumberList, decodeNumber, decodeNumberList))
|
||||
|
||||
encodeNumber :: Enc Integer
|
||||
encodeNumber n = [ "0", "10", "11", "12", "22000", "22001", "22002", "22010", "22011", "22020", "22021", "22022" ] !! (fromIntegral n)
|
||||
encodeListLen :: Enc Int
|
||||
encodeListLen n = [ "0", "1", "220", "2210", "2211", "2212", "2222000", "2222001", "2222002", "2222010", "2222011", "2222012", "2222020", "2222021", "2222022" ] !! n
|
||||
|
||||
encodeList :: Enc a -> Enc [a]
|
||||
encodeList e xs = (encodeListLen (length xs)) ++ (concat $ map e xs)
|
||||
|
||||
encodeFuel = encodeList $ encodeList $ encodeList encodeNumber
|
||||
|
||||
-- Circuit Syntax:
|
||||
-- <inPin>:[<gates>]:<outPin>
|
||||
-- each Pin is either "X" (circuit IN or OUT)
|
||||
@ -57,7 +117,14 @@ block2 = 2:(init input)
|
||||
-- 1: 2L:2R1R0#2R1R,2L0R0#X0R,X0L0#1L0L:1L
|
||||
-- 2: 2R:2R1R0#2L1L,0R2L0#X0R,0LX0#1R0L:1L
|
||||
-- build circuit for needed output
|
||||
build s = let (p, pins) = step (-1) [] (reverse (key ++ (readstream s))) in Circuit p pins where
|
||||
build s = let (p, pins) = step 4 [0,-1,1,6,2,3,5,7] (dropWhile (0 == ) $ reverse (key ++ (readstream s))) in Circuit p pins where
|
||||
step p gates [] = (p, gates)
|
||||
step p gates (x:xs) = let k = length gates in case x of
|
||||
0 -> step k (gates ++ [k+4,k+2,k+5,p,k+1,k+3]) 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
|
||||
|
||||
build1 s = let (p, pins) = step (-1) [] (reverse (key ++ (readstream s))) in Circuit p pins where
|
||||
step p gates [] = (p, gates)
|
||||
step p gates (x:xs) = let k = length gates in case x of
|
||||
0 -> step k (gates ++ [k+4,k+2,k+5,p,k+1,k+3]) xs
|
||||
@ -144,6 +211,7 @@ execfactory (s, f) [] = []
|
||||
execfactory (s, f) (x:xs) = o:execfactory (t, f) xs where (t, o) = f (s, x)
|
||||
|
||||
execcirc circ = execfactory (circfactory circ) input
|
||||
ec circ = execfactory (circfactory circ) (input ++ take 100 (repeat 0))
|
||||
|
||||
readstream :: String -> [Int]
|
||||
readstream = map (\c -> read [c] :: Int)
|
||||
|
Loading…
Reference in New Issue
Block a user