-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintcode.js
198 lines (170 loc) · 4.88 KB
/
intcode.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// ----- OPS -----
const getAdder = (state) => function add(x, y, outputRef) {
state[outputRef] = x + y;
};
const getMultiplier = (state) => function multiply(x, y, outputRef) {
state[outputRef] = x * y;
};
const getIOInputter = (state, ioInputs, quietIO) => function ioIn(outputRef) {
const input = ioInputs.pop();
if (!quietIO) {
console.log('input:', input);
}
state[outputRef] = input;
};
const getIOOutputter = (outputs, quietIO) => function ioOut(x) {
if (!quietIO) {
console.log('output:', x);
}
outputs.push(x);
};
const jumpIfTrue = function jumpIfTrue(toTest, jumpTo) {
return toTest ? jumpTo : undefined;
};
const jumpIfFalse = function jumpIfFalse(toTest, jumpTo) {
return toTest ? undefined : jumpTo;
};
const getLessThanner = (state) => function lessThan(x, y, outputRef) {
state[outputRef] = x < y ? 1 : 0;
};
const getEqualser = (state) => function equals(x, y, outputRef) {
state[outputRef] = x === y ? 1 : 0;
};
const getBaseAdjuster = (state) => function adjustRelBase(x) {
state.relBase += x;
};
// ----- UTILS -----
const parseOpCode = (rawOpCode) => {
const strOpCode = String(rawOpCode);
const opCode = Number(strOpCode.slice(-2));
const paramModes = strOpCode
.slice(0, -2)
.split('')
.reverse()
.map(Number);
return { opCode, paramModes };
};
const getInputParser = (state) => (paramModes, inputs) => inputs
.map((input, i) => [input, paramModes[i] || 0])
.map(([input, mode]) => {
switch (mode) {
case 0: // positional
return state[input];
case 1: // immediate
return input;
case 2: // relative
return state[state.relBase + input];
default:
throw new Error(`Bad input mode: ${mode}`);
}
})
.map(parsed => parsed || 0);
const getOutputParser = (state) => (paramModes, outputs) => outputs
.map((output, i) => [output, paramModes[i] || 0])
.map(([output, mode]) => {
switch (mode) {
case 0: // positional
return output;
case 1: // immediate
return NaN;
case 2: // relative
return state.relBase + output;
default:
throw new Error(`Bad output mode: ${mode}`);
}
});
const getExecutor = (currentRef, debugMode) => (op, ...params) => {
if (debugMode) {
console.log(`>>> (${currentRef}):`, op.name, ...params);
}
const opOutput = op(...params);
return opOutput === undefined
? currentRef + op.length + 1
: opOutput;
};
// ----- RUNNER -----
const getRunner = (initialState, options = {}) => {
const {
debugMode = false,
pauseOnInput = false,
pauseOnOutput = false,
quietIO = false
} = options;
const state = initialState.slice();
state.relBase = 0;
let ref = 0;
const parseInputs = getInputParser(state);
const parseOutputs = getOutputParser(state);
const add = getAdder(state);
const multiply = getMultiplier(state);
const lessThan = getLessThanner(state);
const equals = getEqualser(state);
const adjustRelBase = getBaseAdjuster(state);
return (...ioInputsInOrder) => {
const ioInputs = ioInputsInOrder.slice().reverse();
const outputs = [];
const ioIn = getIOInputter(state, ioInputs, quietIO);
const ioOut = getIOOutputter(outputs, quietIO);
while (true) {
const { opCode, paramModes } = parseOpCode(state[ref]);
const exec = getExecutor(ref, debugMode);
const params = state.slice(ref + 1, ref + 4);
// eslint-disable-next-line no-unused-vars
const [x, y, z] = parseInputs(paramModes, params);
// eslint-disable-next-line no-unused-vars
const [xOut, yOut, zOut] = parseOutputs(paramModes, params);
switch (opCode) {
case 1:
ref = exec(add, x, y, zOut);
break;
case 2:
ref = exec(multiply, x, y, zOut);
break;
case 3:
if (pauseOnInput && ioInputs.length === 0) return outputs;
ref = exec(ioIn, xOut);
break;
case 4:
ref = exec(ioOut, x);
if (pauseOnOutput) return outputs.pop();
break;
case 5:
ref = exec(jumpIfTrue, x, y);
break;
case 6:
ref = exec(jumpIfFalse, x, y);
break;
case 7:
ref = exec(lessThan, x, y, zOut);
break;
case 8:
ref = exec(equals, x, y, zOut);
break;
case 9:
ref = exec(adjustRelBase, x);
break;
case 99:
if (debugMode) console.log('terminate');
if (pauseOnInput) return outputs.concat(null);
if (pauseOnOutput) return null;
return outputs;
default:
throw new Error(`Bad opcode ${opCode} generated from: ${state[ref]}`);
}
}
};
};
module.exports = {
getAdder,
getMultiplier,
getIOInputter,
getIOOutputter,
jumpIfTrue,
jumpIfFalse,
getLessThanner,
getEqualser,
parseOpCode,
getInputParser,
getExecutor,
getRunner
};