-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsciiComputer.mjs
39 lines (31 loc) · 983 Bytes
/
AsciiComputer.mjs
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
import IntcodeComputer from './IntcodeComputer.mjs';
function fromAscii(input) {
return input.split('').map(a => a.charCodeAt(0));
}
function toAscii(output) {
if (Array.isArray(output)) {
return output.map(toAscii).join('');
}
return output <= 255 ? String.fromCharCode(output) : output;
}
export default class AsciiComputer extends IntcodeComputer {
constructor(intcode, input) {
super(intcode, input ? fromAscii(input) : input);
}
_write(chunk, encoding, callback) {
super._write(fromAscii(chunk), encoding, callback);
}
run(input) {
const result = this.doRun(false, input ? fromAscii(input) : input);
return {
...result,
value: toAscii(result.value)
};
}
push(output) {
return super.push(output ? String.fromCharCode(output) : output);
}
}
export function compute(intcode, input) {
return new AsciiComputer(intcode).run(input).value;
}