Ok guys, today's goal is to build a Turing machine simulator. For those that don't know what it is, see the Wikipedia article. The state table we are using today is found at the end of the Formal Definition that's part of that page.
The code will take a sequence of "0" and "1" string characters, an integer representing the character that the machine starts with, and an integer representing the state of the program (in no particular order), and output the final result of the operations on the string, as well as the final position. Examples:
Example 1:
1010 state A(0)
^ (3)
1011 state B(1)
^ (2)
1011 state B(1)
^ (1)
1111 state A(0)
^ (2)
1111 state C(0)
^ (3)
1111 HALT
^ (2)
Example 2:
110100 state B(1)
^ (3)
110100 state B(1)
^ (2)
111100 state A(0)
^ (3)
111100 state C(2)
^ (4)
111110 state B(1)
^ (5)
1111110 state A(0)
^ (6, tape has been extended to right)
1111111 state B(1)
^ (5)
1111111 state B(1)
^ (4)
1111111 state B(1)
^ (3)
1111111 state B(1)
^ (2)
1111111 state B(1)
^ (1)
1111111 state B(1)
^ (0)
01111111 state B(1)
^ (0, tape has been extended to left)
11111111 state A(0)
^ (1)
11111111 state C(2)
^ (2)
11111111 HALT
^ (1)
Misc:
- Your code must properly handle attempts to write into "blank spaces" on the tape, by extending the string as necessary.
- Since the state machine specified does not specify any sort of "blank tape" action, treat all blank values as 0.
- You must count only the method that handles evaluation of a string with initial state, how you output that data is up to you.
- Moving right on the tape is incrementing up (string position 0 is all the way at the left), state 0 is A, state 1 is B, and state 2 is C.
(hopefully) final edit: I offer my most sincere apologies as to the confusion and trouble I've caused with this question: I misread the supplied state table I listed, and got it backwards. I hope you'll forgive me for wasting your time; it was entirely unintentional!
Perl function 101 char
This one was fun to find. Two tricks. It use a hash for the tape, and know what ? A hash is auto-extensible, so no need any more to care about tape boundaries. The other trick is for combining both read and write of the cell accessed. Just had to change internal conventions 0 and space means 0 and any other value means 1. These two tricks implies some trivial decoding of output, but I believe it's ok. I also not counted the final semi-colon in my function as gnibbler didn't counted his in his golfscript.
If someone is interested I can also post my other tries. They are a bit longer but uses fun tricks. One is regex based for instance and works directly with tape as string another one is a kind of bit-fu.
Perl function 112 char
I counted the function only and it takes a string, a state num and a position in that order as specified. The function returns new tape state as an array.
Another variant 106 char
It is not clear if this one is cheating or not. It gives correct results and automatically extends tape (no fixed limit), but to avoid testing if it is necessary or not to extend tape it does so every step and adjust index.
Another variant 98 char
This one is also on the merge but in a different way. It just use globals to pass parameters inside the function. Hence you set your variables outside the function instead of inside. Thus removing 14 characters from the function body.