Skip to content

Instantly share code, notes, and snippets.

@linuskmr
Last active May 10, 2021 16:44
Show Gist options
  • Save linuskmr/2db80ebc839a06ce0cc387a264202fe9 to your computer and use it in GitHub Desktop.
Save linuskmr/2db80ebc839a06ce0cc387a264202fe9 to your computer and use it in GitHub Desktop.
Converts transitions of a turing machine into a bitstream
transitions = [
// ['state', 'symbol', 'new_state', 'new_symbol', 'direction']
['q0', 'a', 'q1', 'b', 'r'],
['q1', 'b', 'q2', 'c', 'l']
]
console.log(
// Map each transition to its bits started by ##
transitions.map(transition => '##' + transition
// Map each parameter in the transition to its bits
.map(param => param.split('')
.map((char, i) => param
// Convert each char into its byte value
.charCodeAt(i)
// Convert each byte value to bits
.toString(2)
)
// Concat multi-byte strings
.join('')
)
// Add separator
.join('#')
)
// Concat words and split into char array
.join('').split('')
// Map each char to its escaped value
.map(char => {
return {
'0': '00',
'1': '01',
'#': '11'
}[char]
})
.join('')
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment