-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathparse-instructions.js
66 lines (58 loc) · 1.85 KB
/
parse-instructions.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
import { input } from './input.js';
function translateOp(op) {
// inp a - Read an input value and write it to variable a.
if (op.startsWith('inp')) {
let [, a] = op.split(' ');
return `${a} = input;`;
}
// add a b - Add the value of a to the value of b, then store the result in variable a.
if (op.startsWith('add')) {
let [, a, b] = op.split(' ');
return `${a} += ${b};`;
}
// mul a b - Multiply the value of a by the value of b, then store the result in variable a.
if (op.startsWith('mul')) {
let [, a, b] = op.split(' ');
return `${a} *= ${b};`;
}
// div a b - Divide the value of a by the value of b, truncate the result to an integer, then store the result in variable a. (Here, "truncate" means to round the value toward zero.)
if (op.startsWith('div')) {
let [, a, b] = op.split(' ');
return `${a} = Math.trunc(${a} / ${b});`;
}
// mod a b - Divide the value of a by the value of b, then store the remainder in variable a. (This is also called the modulo operation.)
if (op.startsWith('mod')) {
let [, a, b] = op.split(' ');
return `${a} %= ${b};`;
}
// eql a b - If the value of a and b are equal, then store the value 1 in variable a. Otherwise, store the value 0 in variable a.
if (op.startsWith('eql')) {
let [, a, b] = op.split(' ');
return `${a} = ${a} === ${b} ? 1 : 0;`;
}
}
class Command {
constructor(raw) {
this.raw = raw;
}
toString() {
return `\t// ${this.raw}\n\t${translateOp(this.raw)}`;
}
}
let current_func = [];
let program = [];
for (let line of input) {
if (current_func.length && line.startsWith('inp')) {
program.push(`function part${program.length + 1}(input) {
${current_func.join('\n\n')}
}`);
current_func = [];
}
current_func.push(new Command(line));
}
program.push(`function part${program.length + 1}(input) {
${current_func.join('\n\n')}
}`);
for (let p of program) {
console.log(p + '\n');
}