-
Notifications
You must be signed in to change notification settings - Fork 0
/
hrm.js
167 lines (148 loc) · 3.75 KB
/
hrm.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
'use strict';
function equals(a, b) {
return JSON.stringify(a) === JSON.stringify(b);
}
function clone(o) {
return JSON.parse(JSON.stringify(o));
}
function nZeros(n) {
const arr = new Array(n);
arr.fill(0);
return arr;
}
function randomInt(n) {
return Math.floor(Math.random() * n);
}
function randomArray(arr) {
const n = arr.length;
return arr[randomInt(n)];
}
function isNumber(n) {
return typeof n === 'number';
}
function isInteger(n) {
if (typeof n !== 'number') {
return false;
}
return n === Math.floor(n);
}
/*
IN .....* ->
OUT -> *.....
*/
const MAX_STEPS = 40;
const OPS = {
inbox: function() {
if (this.in.length === 0) {
throw new Error('inbox is empty!');
}
this.hand = this.in.pop();
},
outbox: function() {
if (this.hand === undefined) {
throw new Error('outbox requires hand!');
}
this.out.unshift(this.hand);
},
copyfrom: function(index) {
if (!isInteger(index)) {
throw new Error('copyfrom argument 0 (index) must be an integer!');
}
this.hand = this.cells[index];
},
copyto: function(index) {
if (!isInteger(index)) {
throw new Error('copyto argument 0 (index) must be an integer!');
}
this.cells[index] = this.hands;
},
jump: function(lineNo) {
if (!isInteger(lineNo)) {
throw new Error('jump argument 0 (lineNo) must be an integer!');
}
this.lineNo = lineNo - 1;
},
jump0: function(lineNo) {
if (!isInteger(lineNo)) {
throw new Error('jump0 argument 0 (lineNo) must be an integer!');
}
if (this.hand === 0) {
this.lineNo = lineNo - 1;
}
},
jumpneg: function(lineNo) {
if (!isInteger(lineNo)) {
throw new Error('jumpneg argument 0 (lineNo) must be an integer!');
}
if (this.hand < 0) {
this.lineNo = lineNo - 1;
}
},
add: function(index) {
if (!isInteger(index)) {
throw new Error('add argument 0 (index) must be an integer!');
}
if (!isNumber(this.hand)) {
throw new Error('hand must have a number!');
}
if (!isNumber(this.cells[index])) {
throw new Error(`cell #${index} must have a number!`);
}
this.hand += this.cells[index];
},
sub: function(index) {
if (!isInteger(index)) {
throw new Error('add argument 0 (index) must be an integer!');
}
if (!isNumber(this.hand)) {
throw new Error('hand must have a number!');
}
if (!isNumber(this.cells[index])) {
throw new Error(`cell #${index} must have a number!`);
}
this.hand -= this.cells[index];
}
};
function step(level, cmdName, arg0) {
const fn = OPS[cmdName];
if (!fn) {
throw new Error(`opcode ${cmdName} is unsupported!`);
}
fn.call(level, arg0);
}
function printLevel(level) {
log(`
in: ${JSON.stringify(level.in)}
cells: ${JSON.stringify(level.cells)}
out: ${JSON.stringify(level.out)}
hand: ${level.hand !== undefined ? JSON.stringify(level.hand) : ''}
\n`);
}
function* run(level, program) {
level.lineNo = 0;
level.steps = 0;
const commands = program.split('\n').map((l) => {
const tokens = l.split(' ');
return tokens.map((t, i) => (i === 0 ? t : parseInt(t, 10)));
});
yield level;
// so it does not run indefinetely
while (level.steps < MAX_STEPS) {
const [cmdName, arg0] = commands[level.lineNo];
try {
step(level, cmdName, arg0);
} catch (ex) {
if (ex) {
log(`ERROR: ${ex}\n`);
return level;
}
}
level.steps += 1;
level.lineNo += 1;
if (level.successReached(level)) {
level.completed = true;
return level;
}
yield level;
}
}