-
Notifications
You must be signed in to change notification settings - Fork 0
/
19.ts
93 lines (87 loc) · 2.8 KB
/
19.ts
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
const fs = require('fs');
interface Instruction {
opcode: string;
args: Array<number>;
}
type Program = Array<Instruction>;
function runProgram(program: Program, r0: number, ip_reg: number) {
let regs = [r0, 0, 0, 0, 0, 0];
let ip = 0;
let n = 0;
while(ip >= 0 && ip < program.length) {
n += 1;
if (ip_reg >= 0) {
regs[ip_reg] = ip;
}
const inst = program[ip];
switch(inst.opcode) {
case 'addr':
regs[inst.args[2]] = regs[inst.args[0]] + regs[inst.args[1]];
break;
case 'addi':
regs[inst.args[2]] = regs[inst.args[0]] + inst.args[1];
break;
case 'mulr':
regs[inst.args[2]] = regs[inst.args[0]] * regs[inst.args[1]];
break;
case 'muli':
regs[inst.args[2]] = regs[inst.args[0]] * inst.args[1];
break;
case 'banr':
regs[inst.args[2]] = regs[inst.args[0]] & regs[inst.args[1]];
break;
case 'bani':
regs[inst.args[2]] = regs[inst.args[0]] & inst.args[1];
break;
case 'borr':
regs[inst.args[2]] = regs[inst.args[0]] | regs[inst.args[1]];
break;
case 'bori':
regs[inst.args[2]] = regs[inst.args[0]] | inst.args[1];
break;
case 'setr':
regs[inst.args[2]] = regs[inst.args[0]];
break;
case 'seti':
regs[inst.args[2]] = inst.args[0];
break;
case 'gtir':
regs[inst.args[2]] = (inst.args[0] > regs[inst.args[1]]) ? 1 : 0;
break;
case 'gtri':
regs[inst.args[2]] = (regs[inst.args[0]] > inst.args[1]) ? 1 : 0;
break;
case 'gtrr':
regs[inst.args[2]] = (regs[inst.args[0]] > regs[inst.args[1]]) ? 1 : 0;
break;
case 'eqir':
regs[inst.args[2]] = (inst.args[0] === regs[inst.args[1]]) ? 1 : 0;
break;
case 'eqri':
regs[inst.args[2]] = (regs[inst.args[0]] === inst.args[1]) ? 1 : 0;
break;
case 'eqrr':
regs[inst.args[2]] = (regs[inst.args[0]] === regs[inst.args[1]]) ? 1 : 0;
break;
}
if (ip_reg >= 0) {
ip = regs[ip_reg];
}
//console.log(inst, regs);
ip++;
}
console.log(n, regs);
}
let program: Program = fs.readFileSync(process.argv[2], 'ascii')
.split("\n")
.filter(line => line.length > 0)
.map(line => {
let parts = line.split(" ");
const opcode = parts.shift();
return { opcode: opcode, args: parts.map(arg => parseInt(arg, 10)) }
});
let ip_reg;
if (program[0].opcode == '#ip') {
ip_reg = program.shift().args[0];
}
runProgram(program, parseInt(process.argv[3], 10), ip_reg);