(*
	(c) 1998, 2008 Alex Kontorovich
*)

(* 

This program computes the Audioactive decay sequence MOD 2. So the next line after

11100110

is (look-and-say: 
three 1's, two 0's, two 1's, one 0 = 31202110 -> (MOD 2) 11000110 )

11000110 

*)

(* The parameters *)

x={1}; (* Seed *)

iMax=1024; (* number of lines to make *)


(* Look-and-say MOD 2 *)
F[x_]:=Module[
{i,answer,digit,count},
answer={}; 
digit=x[[1]]; 
count=1; 
For[i=2,i<=Length[x],i++,
If[
 x[[i]]==digit,
count=count+1
,
(* else *)
AppendTo[answer,Mod[count,2]];
AppendTo[answer,digit];
digit=x[[i]];
count=1;
]
];
AppendTo[answer,Mod[count,2]];
AppendTo[answer,digit];
answer
];

(* Initialize *)
y={x};
(* Run the code *)
For[i=1,i<iMax,i=i+1,
AppendTo[y,F[y[[i]]]]
];

(*
Show y as sequence of 0's and 1's (pixels) 

The picture comes out better right-justified.

*)

ArrayPlot[PadLeft[y],PlotRange->All]

(* Another seed *)

x={1,1,0,1}; (* Seed *)

iMax=1024; (* number of lines to make *)

(* Initialize *)
y={x};
(* Run the code *)
For[i=1,i<iMax,i=i+1,
AppendTo[y,F[y[[i]]]]
];

(* show y as sequence of 0's and 1's (pixels) *)
ArrayPlot[PadLeft[y]]

(* 

This time we'll read Hebrew (right-to-left), while writing in English

100111

becomes (three 1s, two 0s, one 1 = 312011 (mod 2) = 110011

*)

x={1,0}; (* Seed *)

iMax=1024; (* number of lines to make *)


(* Look-and-say MOD 2 *)
F[x_]:=Module[
{i,answer,digit,count},
answer={}; 
digit=Last[x]; 
count=1; 
For[i=Length[x]-1,i≥1,i--,
If[
 x[[i]]==digit,
count=count+1
,
(* else *)
AppendTo[answer,digit];
AppendTo[answer,Mod[count,2]];
digit=x[[i]];
count=1;
]
];
AppendTo[answer,digit];
AppendTo[answer,Mod[count,2]];
answer
];

(* Initialize *)
y={x};
(* Run the code *)
For[i=1,i<iMax,i=i+1,
AppendTo[y,F[y[[i]]]]
];

(*
Show y as sequence of 0's and 1's (pixels) 

*)

ArrayPlot[PadLeft[y],PlotRange->All]

(* So that gives a Sierpinski gasket! *)

(*

Just a quick note on the analysis:

Going back to the original sequence, it's better to change notation to (all but the first line must have even length);

A=11;
T=10;
C=01;
G=00;

Then if in a string one sees ...AT... = ...1110..., there must actually have been ...(0)1110..., so this decays to ...11...=...A... ;

This gives the rules (and similar ones -- note that A/C cannot be followed by A/C, and similarly with T/G);

AT = (0)1110 -> 11 = A;
AG = (0)1100 -> 01 = C;
TA = 1011 -> 10 = T;
TC = 1001 -> 00 = G;
CT = 0110 -> 01 = C;
CG = 0100 -> 11 = A;
GA = (1)0011 -> 00 = G;
GC = (1)0001 -> 10 = T;

*)