[SPOILER] Perl Quiz of the Week #24 (Turing Machine simulation)

"Tor Fuglerud" <[email protected]> Fri, 17 Sep 2004 16:36:47 +0200
Newsgroups gmane.comp.lang.perl.qotw.discuss
Organization UiO
Message-ID <[email protected]>
This is solution doesn't check for errors or comments in the .tm file,  
otterwise it should be acording to specks. Initialy tape is one _ , the  
third line in the while(1) loop grows the tape as needed by push or  
splice. Yeah, and I tried to keep things simple by naming everything p if  
possible.

:-)

#!/local/bin/perl5 -l
open I,shift;
while (<I>) {
     @p = split;
     $s = $p[0] if !$s;
     ($p{$p[0]})?($p{$p[0]}->{$p[1]}=[@p[2..4]]):($p{$p[0]}={$p[1]=>[@p[2..4]]})}
@p = @ARGV?(split '',shift):('_');

while(1){
     ($a,$p[$p],$b)=@{$p{$s}->{$p[$p]}};
     last if!$p{$a};
     ($b eq'R')?(++$p>$#p?(push @p,'_'):''):(!$p?splice @p,0,0,'_':$p--);
     $s=$a}
print @p


-- 
Tor Fuglerud