-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpart-one.js
122 lines (98 loc) · 2.49 KB
/
part-one.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// const fs = require('fs')
const { parse } = require('./turing-lang/lib/turing-lang');
/*
Begin in state A.
Perform a diagnostic checksum after 12919244 steps.
In state A:
If the current value is 0:
- Write the value 1.
- Move one slot to the right.
- Continue with state B.
If the current value is 1:
- Write the value 0.
- Move one slot to the left.
- Continue with state C.
In state B:
If the current value is 0:
- Write the value 1.
- Move one slot to the left.
- Continue with state A.
If the current value is 1:
- Write the value 1.
- Move one slot to the right.
- Continue with state D.
In state C:
If the current value is 0:
- Write the value 1.
- Move one slot to the right.
- Continue with state A.
If the current value is 1:
- Write the value 0.
- Move one slot to the left.
- Continue with state E.
In state D:
If the current value is 0:
- Write the value 1.
- Move one slot to the right.
- Continue with state A.
If the current value is 1:
- Write the value 0.
- Move one slot to the right.
- Continue with state B.
In state E:
If the current value is 0:
- Write the value 1.
- Move one slot to the left.
- Continue with state F.
If the current value is 1:
- Write the value 1.
- Move one slot to the left.
- Continue with state C.
In state F:
If the current value is 0:
- Write the value 1.
- Move one slot to the right.
- Continue with state D.
If the current value is 1:
- Write the value 1.
- Move one slot to the right.
- Continue with state A.
*/
// CURRENT_STATE READ_SYMBOL => NEXT_STATE WRITTEN_SYMBOL [LEFT | RIGHT]
/**
* Q0: State A
* Q1: State B
* Q2: State C
* Q3: State D
* Q4: State E
* Q5: State F
*/
const MACHINE_CODE = `
Q0 0 => Q1 1 RIGHT
Q0 1 => Q2 0 LEFT
Q1 0 => Q0 1 LEFT
Q1 1 => Q3 1 RIGHT
Q2 0 => Q0 1 RIGHT
Q2 1 => Q4 0 LEFT
Q3 0 => Q0 1 RIGHT
Q3 1 => Q1 0 RIGHT
Q4 0 => Q5 1 LEFT
Q4 1 => Q2 1 LEFT
Q5 0 => Q3 1 RIGHT
Q5 1 => Q0 1 RIGHT
`;
let machine = parse(MACHINE_CODE);
machine.running = true;
// console.log('Step 0', machine.tape.data);
for (let i = 0; i < 12919244; i++) {
if (i % 7919 === 0) {
process.stdout.write(i + '...\r');
}
machine.step();
// console.log('Step ' + (i + 1), machine.tape.data);
}
let values = Object.values(machine.tape.data).filter(n => n === '1');
console.log('\n')
console.log(values.length)
console.log('')
// fs.writeFileSync('output2.json', JSON.stringify(values));