Skip to content

Instantly share code, notes, and snippets.

@iolo
Created April 21, 2023 15:54
Show Gist options
  • Save iolo/b5c3fcdf286b5ded38911b855463006d to your computer and use it in GitHub Desktop.
Save iolo/b5c3fcdf286b5ded38911b855463006d to your computer and use it in GitHub Desktop.
proof-of-concept Turing Machine Emulator
import { TuringMachine } from './turing-machine.mjs';
const transitionFunction = {
q0: {
0: ['q0', '0', 'R'],
1: ['q0', '1', 'R'],
END: ['q1', 'END', 'L'],
},
q1: {
0: ['q2', '1', 'R'],
1: ['q1', '0', 'L'],
},
};
const finalStates = ['q2'];
const input = ['1', '0', '1']; // 이진수 101
const tm = new TuringMachine(input, 'q0', 0, transitionFunction, finalStates);
const output = tm.run();
console.log(output); // 출력: [ '1', '1', '0' ] (이진수 110)
import { TuringMachine } from './turing-machine.mjs';
const transitionFunction = {
q0: {
...Array.from({ length: 26 }, (_, i) => [
String.fromCharCode(65 + i),
['q0', String.fromCharCode(65 + ((i + 3) % 26)), 'R'],
]).reduce((obj, [k, v]) => ({ ...obj, [k]: v }), {}),
// 휴먼 코멘트: 위의 줄은 아래와 같은 튜링머신 코드를 생성
//'A': ['q0', 'D', 'R']
//'B': ['q0', 'E', 'R']
//'B': ['q0', 'F', 'R']
//...
//'X': ['q0', 'A', 'R']
//'Y': ['q0', 'B', 'R']
//'Z': ['q0', 'C', 'R']
//'END': ['q1', 'END', 'R'],
},
};
const finalStates = ['q1'];
const input = ['A', 'B', 'Y', 'Z']; // 암호화할 문자열
const tm = new TuringMachine(input, 'q0', 0, transitionFunction, finalStates);
const output = tm.run();
console.log(output); // 출력: [ 'D', 'E', 'B', 'C' ] (암호화된 문자열)
import { TuringMachine } from './turing-machine.mjs';
const transitionFunction = {
q0: {
...Array.from({ length: 10 }, (_, i) => [String(i), ['q0', String(i), 'R']]).reduce(
(obj, [k, v]) => ({ ...obj, [k]: v }),
{}
),
// 휴먼 코멘트: 위의 줄은 아래와 같은 튜링머신 코드를 생성
// '0': ['q0', '0', 'R'],
// '1': ['q0', '1', 'R'],
// ...
// '9': ['q0', '9', 'R'],
'END': ['q1', 'END', 'L'],
},
q1: {
...Array.from({ length: 9 }, (_, i) => [String(i), ['q2', String(i + 1), 'R']]).reduce(
(obj, [k, v]) => ({ ...obj, [k]: v }),
{}
),
// 휴먼 코멘트: 위의 줄은 아래와 같은 튜링머신 코드를 생성
// '0': ['q2', '1', 'R'],
// '1': ['q2', '2', 'R'],
// ...
// '8': ['q2', '9', 'R'],
'9': ['q1', '0', 'L'],
},
};
const finalStates = ['q2'];
const input = ['9', '0', '9']; // 10진수 909
const tm = new TuringMachine(input, 'q0', 0, transitionFunction, finalStates);
const output = tm.run();
console.log(output); // 출력: [ '9', '1', '0' ] (10진수 910)
import { TuringMachine } from './turing-machine.mjs';
const transitionFunction = {
q0: {
...Array.from({ length: 26 }, (_, i) => [
String.fromCharCode(65 + i),
['q0', String.fromCharCode(65 + ((i + 3) % 26)), 'R'],
]).reduce((obj, [k, v]) => ({ ...obj, [k]: v }), {}),
// 휴먼 코멘트: 위의 줄은 아래와 같은 튜링머신 코드를 생성
//'A': ['q0', 'D', 'R']
//'B': ['q0', 'E', 'R']
//'B': ['q0', 'F', 'R']
//...
//'X': ['q0', 'A', 'R']
//'Y': ['q0', 'B', 'R']
//'Z': ['q0', 'C', 'R']
//'END': ['q1', 'END', 'R'],
},
};
const finalStates = ['q1'];
const input = ['A', 'B', 'Y', 'Z']; // 암호화할 문자열
const tm = new TuringMachine(input, 'q0', 0, transitionFunction, finalStates);
const output = tm.run();
console.log(output); // 출력: [ 'D', 'E', 'B', 'C' ] (암호화된 문자열)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment