Skip to content

Instantly share code, notes, and snippets.

@ryohey
Created January 13, 2016 16:11
Show Gist options
  • Select an option

  • Save ryohey/2fc4037616056196bedb to your computer and use it in GitHub Desktop.

Select an option

Save ryohey/2fc4037616056196bedb to your computer and use it in GitHub Desktop.
The Wolfram 2, 3 Turing Machine in CoffeeScript
logTm = (tm) ->
text = tm.colors
.map (s, i) -> if i is tm.pos then "%c" + ["▲", "▼"][tm.state] else "%c "
.join ""
style = tm.colors.map (c) -> "background-color:" + ["white", "yellow", "orange"][c] + ";"
console.log.apply console, [text].concat style
# [state, color] = [state, color, offset]
rule = {}
rule[[0, 2]] = [0, 1, -1]
rule[[0, 1]] = [0, 2, -1]
rule[[0, 0]] = [1, 1, 1]
rule[[1, 2]] = [0, 0, 1]
rule[[1, 1]] = [1, 2, 1]
rule[[1, 0]] = [0, 2, -1]
stepTm = (tm) ->
r = rule[[tm.state, tm.colors[tm.pos]]]
tm.state = r[0]
tm.colors[tm.pos] = r[1]
tm.pos += r[2]
tm =
colors: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
pos: 3
state: 0
for _ in [0..50]
logTm tm
stepTm tm
@ryohey
Copy link
Copy Markdown
Author

ryohey commented Jan 13, 2016

Result

tm23

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment