Skip to content

Instantly share code, notes, and snippets.

@ytaki0801
Created December 20, 2022 07:59
Show Gist options
  • Save ytaki0801/d091b52e02a1c4bac7b1e4a49dce63b2 to your computer and use it in GitHub Desktop.
Save ytaki0801/d091b52e02a1c4bac7b1e4a49dce63b2 to your computer and use it in GitHub Desktop.
(2,3) Turing Machine as an UTM with an infinite tape
# (2,3) Turing Machine as an UTM
from time import sleep
# (state,symbol) => (state,symbol,head)
delta = {(0,0): (1,1, 1),
(0,1): (0,2,-1),
(0,2): (0,1,-1),
(1,0): (0,2,-1),
(1,1): (1,2, 1),
(1,2): (0,0, 1)}
state, tape, head = 0, [0], 0
ds = {0: ' ', 1: ' ', 2: '■'}
while True:
t = delta[(state,tape[head])]
state = t[0]
tape[head] = t[1]
head += t[2]
if head >= len(tape):
tape.append(0)
elif head < 0:
tape.insert(0, 0)
head += 1
for s in tape: print(ds[s], end='')
print()
sleep(0.01)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment